JSI Tip 4193. How can I determine the length of a string?

Jerold Schulman

October 7, 2001

1 Min Read
ITPro Today logo in a gray background | ITPro Today


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

Demonstration of usage:

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?



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.

You May Also Like