Counting string length
Posted: 11 Feb 2013 14:46
Checking long of word. I am doing something wrong because I must admit I do not understand in 100% how it works. I would like to enter a word and Batch will show its length so I converted that script from library:
to:
but doesn't work, how to fix it ?
Code: Select all
( SETLOCAL ENABLEDELAYEDEXPANSION
set "str=A!%~1!"&rem keep the A up front to ensure we get the length and not the upper bound
rem it also avoids trouble in case of empty string
set "len=0"
for /L %%A in (12,-1,0) do (
set /a "len|=1<<%%A"
for %%B in (!len!) do if "!str:~%%B,1!"=="" set /a "len&=~1<<%%A"
)
)
( ENDLOCAL & REM RETURN VALUES
IF "%~2" NEQ "" SET /a %~2=%len%
)
EXIT /b
to:
Code: Select all
@echo off
set /p str=
( SETLOCAL ENABLEDELAYEDEXPANSION
set "str=A!%~1!"
set "len=1"
for /L %%A in (12,-1,0) do (
set /a "len|=1<<%%A"
for %%B in (!len!) do if "!str:~%%B,1!"=="" set /a "len&=~1<<%%A"
)
)
( ENDLOCAL
IF "%~2" NEQ "" SET /a %~2=%len%
)
echo Show: %len%
pause > nul
but doesn't work, how to fix it ?