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


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 off
setlocal
set /a $len=0
set $string=####%1####
set $string=%$string:####"=%
set $string=%$string:"####=%
if "%$string%" EQU "" goto end
set $string=%$string:####=%#*#*
:loop
call $poslen "%$string%" %$len% 4
if "%$substring%" EQU "#*#*" goto end
set /a $len=%$len% + 1
goto loop
:end
endlocal&set /a $len=%$len%
REM end $Length

Demonstration of usage:

Running:

call $length "%UserName%"
@echo User name %UserName% has %$len% characters.

returns

User 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?



Hide comments

Comments

  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
Publish