Originally posted by zts59 at 2006-8-23 19:36:
set char=
set /p char= enter:
:test
set n=0
set "char0=!char:~%n%,1!"
if %char0%="" goto end
set /A n="n+1"
echo %char0%
goto test
:end
pause
想达 ...
1.逻辑上的错误:
set n=0
在后面的goto test后,它会重新把n的值设置为0,就算你其他代码全部正确,也只会陷入一个死循环中,不停地显示第一个字符。
正解:
把它放到test标签前面去。
2.语法上的错误:
if %char0%="" goto end
正解:
if "char0%" == "" goto end
3.命令上的错误:
没有使用启用延迟环境变量的命令:setlocal ENABLEDELAYEDEXPANSION,导致后面的!!所包含的变量不被识别,而被当作是字符串。
正解:
在前面加上
setlocal ENABLEDELAYEDEXPANSION
把你的代码略做修改如下:
@echo off
setlocal ENABLEDELAYEDEXPANSION
set n=0
set char=
set /p char= enter:
:test
set "char0=!char:~%n%,1!"
if "%char0%" == "" goto end
set /A n="n+1"
echo %char0%
goto test
:end
pause
用来处理
不包含敏感字符的字符串,基本上足够了。
Originally posted by zts59 at 2006-8-23 19:36:
set char=
set /p char= enter:
:test
set n=0
set "char0=!char:~%n%,1!"
if %char0%="" goto end
set /A n="n+1"
echo %char0%
goto test
:end
pause
Want to achieve...
1. Logical error:
The line
set n=0
is after the subsequent goto test, which will reset the value of n to 0 again. Even if all other code is correct, it will fall into an infinite loop, continuously displaying the first character.
Correct solution:
Move it in front of the test label.
2. Syntax error:
if %char0%="" goto end
Correct solution:
if "char0%" == "" goto end
3. Command error:
The command to enable delayed environment variable expansion, setlocal ENABLEDELAYEDEXPANSION, is not used, resulting in the variables contained in the subsequent!! not being recognized and being treated as strings.
Correct solution:
Add
setlocal ENABLEDELAYEDEXPANSION at the front.
Modify your code slightly as follows:
@echo off
setlocal ENABLEDELAYEDEXPANSION
set n=0
set char=
set /p char= enter:
:test
set "char0=!char:~%n%,1!"
if "%char0%" == "" goto end
set /A n="n+1"
echo %char0%
goto test
:end
pause
Used to handle
strings without sensitive characters, which is basically sufficient.