@echo off setlocal EnableDelayedExpansion :: 計算字符串的長度 :: :: :began set mystr= set /p mystr=輸入要計算長度的字符串(直接回車退出): if not defined mystr goto :eof for /l %%i in (0,1,1000) do if "!mystr:~%%i,1!"=="" set strlen=%%i goto :_endfor :_endfor ::echo 字符串: "%mystr%" echo 長 度: %strlen% set strlen= goto began 該方案的缺陷是:當(dāng)字符串長度超過1000的時候,將不能探測到;不過變量能接收的長度好象還不能超過1000 另外一種方案: @echo off :: Based on willsort's codes code by JM 2006-10-9 CMD@XP :: 出處:http://www.cn-dos.net/forum/viewthread.php?tid=19819 set mystr= set /p mystr=輸入要計算長度的字符串(直接回車退出): if not defined mystr goto :eof set count=0 setlocal enabledelayedexpansion :intercept set /a count+=1 for /f %%i in ("%count%") do if not "!mystr:~%%i,1!"=="" goto intercept echo 長 度: %count% pause goto :eof @echo off :: :: :began set /p mystr=輸入要計算長度的字符串: echo %mystr:~0,1% | findstr /i "q" 1>nul 2>nul exit rem 輸入q退出 call :bflen "%mystr%" rem 這兩句調(diào)用bflen,返回長度到變量ret中。 echo 字符串: "%mystr%" echo 長 度: %ret% goto began :bflen rem 真麻煩,初始化要單獨拿出來。 set num= set str= set ret= set str=%1 :len rem 計算字符串長度 set str=%str:~0,-1% set /a num = %num% + 1 if defined str goto len set /a ret = %num% - 2 set num= goto :eof