作者:chishingchan | 时间:2008-04-11 23:58 | 标题:[已解决]优化一下计算字符串长度的代码,谢谢!
@echo off
setlocal enabledelayedexpansion
set count=0
set var=abcdefghijklmnopqrstuvwxyz
:count
set /a count+=1
for /f %%i in ("%count%") do if not "!var:~%%i,1!"=="" goto count
echo 字符串 %var% 的长度是 %count% 个字符。
pause
计算字符串长度的例子,我不想使用GOTO语句而想直接在FOR下完成,行不?
[
Last edited by chishingchan on 2008-4-12 at 05:14 PM ]
作者:ThinKing | 时间:2008-04-12 12:43 | 标题:行
@echo off & Setlocal enabledelayedexpansion
set "var=abcdefghijklmnopqrstuvwxyz"
for /L %%i in (1,1,10000) do (
set count=%%i
if "!var:~%%i,1!"=="" call :end & exit /b 0
)
goto:eof
:end
echo 字符串 %var% 的长度是 %count% 个字符。
作者:chishingchan | 时间:2008-04-12 15:43 | 标题:谢谢,我再优化一下:
总结:
@echo off&Setlocal enabledelayedexpansion
set var=abcdefghijklmnopqrstuvwxyz 1234567890
for /l %%i in (1,1,10000) do if "!var:~%%i,1!"=="" echo %%i&pause>nul
[
Last edited by chishingchan on 2008-4-12 at 03:51 PM ]
作者:lxmxn | 时间:2008-04-13 07:03
现实结果之后还没完呢,继续优化一下?
作者:chishingchan | 时间:2008-04-14 13:03 | 标题:上个优化是错误的! 再次优化:
@echo off&setlocal enabledelayedexpansion&set string=%1
for /l %%i in (1,1,65535) do if "!string:~%%i,1!"=="" echo 字符串 %string% 的长度是 %%i 个字符。&pause>nul&exit /b 0
echo 没有参数字符串,长度是 0 个字符。&pause>nul
[
Last edited by chishingchan on 2008-4-18 at 09:17 AM ]