Sign up for the ITPro Today newsletter
Stay on top of the IT universe with commentary, news analysis, how-to's, and tips delivered to your inbox daily.
October 7, 2001
NOTE: See tip 9470 A better way to determine the length of a string.
Using $PosLen, I have scripted $length.bat.
The usage syntax is:
call $length string
which returns the $len numeric environment variable.
$length.bat contains:
@echo offsetlocalset /a $len=0set $string=####%1####set $string=%$string:####"=%set $string=%$string:"####=%if "%$string%" EQU "" goto endset $string=%$string:####=%#*#*:loopcall $poslen "%$string%" %$len% 4if "%$substring%" EQU "#*#*" goto endset /a $len=%$len% + 1goto loop:endendlocal&set /a $len=%$len%REM end $Length
Running:call $length "%UserName%"@echo User name %UserName% has %$len% characters.returnsUser name Jerry has 5 characters.
NOTE: See tips:
4192 How can I determine if a string contains a specific sub-set of characters?
4194 How do I determine the position of a sub-string in a string?
You May Also Like