欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

計算字符串的長度的多種解決方案集錦

 更新時間:2009年05月26日 01:13:23   作者:  
計算字符串的長度的批處理,多做處理方案,大家根據(jù)需要選擇。
復(fù)制代碼 代碼如下:

@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

另外一種方案:

復(fù)制代碼 代碼如下:

@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

相關(guān)文章

最新評論