似乎没有一种简单的方法来获取批处理文件中字符串的长度。例如,
SET MY_STRING=abcdefg
SET /A MY_STRING_LEN=???如何找到MY_STRING的字符串长度?
如果string length函数处理字符串中所有可能的字符,包括转义字符,就会得到加分,比如:!%^^()^!。
发布于 2015-04-14 17:50:52
@echo off
:: warning doesn't like * ( in mystring
setlocal enabledelayedexpansion
set mystring=this is my string to be counted forty one
call :getsize %mystring%
echo count=%count% of "%mystring%"
set mystring=this is my string to be counted
call :getsize %mystring%
echo count=%count% of "%mystring%"
set mystring=this is my string
call :getsize %mystring%
echo count=%count% of "%mystring%"
echo.
pause
goto :eof
:: Get length of mystring line ######### subroutine getsize ########
:getsize
set count=0
for /l %%n in (0,1,2000) do (
set chars=
set chars=!mystring:~%%n!
if defined chars set /a count+=1
)
goto :eof
:: ############## end of subroutine getsize ########################https://stackoverflow.com/questions/5837418
复制相似问题