Page 1 of 1

How to get string length (two possibilites)

Posted: 28 Jun 2010 05:09
by miskox
Hi all,

here are two possibilites to determine the string's length. I hope you like them.

First version sets a temporary variable and when it becomes undefined the counter determines the string's length. Of course string is passed as %1 (first parameter).

Code: Select all

@echo off
set strorg=%~1
set strtmp=
set /a cnt=0

:1
call set strtmp=%%strorg:~%cnt%,1%%
if defined strtmp set /a cnt=cnt+1&&goto :1
echo String length is %cnt%



Second version sets a temporary variable character by character and when it becomes the same as source string we know the string's length. Of course string is passed as %1 (first parameter). This version does not handle special characters (" etc.).

Code: Select all

@echo off
set strorg=%~1
set strtmp=
set /a cnt=0
:0
call set strtmp=%strtmp%%%strorg:~%cnt%,1%%
if not "[%strorg%]"=="[%strtmp%]" set /a cnt=cnt+1&&goto :0
set /a strlen=cnt+1
echo String length is %strlen%


Saso

Re: How to get string length (two possibilites)

Posted: 28 Jun 2010 10:43
by aGerman
Good job.

Here you will find another possibility.

Similar:

Code: Select all

@echo off &setlocal
set var=TestString


call :strlen "%var%" len
echo The length of %var% is %len%.
pause
goto :eof

:strlen
setlocal
set "str=_%~1"
set /a len=512
set /a n=512
call set "temp=%%str:~%len%,1%%"
if defined temp (set /a len+=%n%/2) else set /a len-=%n%/2
set /a n=%n%/2
call set "temp=%%str:~%len%,1%%"
if defined temp (set /a len+=%n%/2) else set /a len-=%n%/2
set /a n=%n%/2
call set "temp=%%str:~%len%,1%%"
if defined temp (set /a len+=%n%/2) else set /a len-=%n%/2
set /a n=%n%/2
call set "temp=%%str:~%len%,1%%"
if defined temp (set /a len+=%n%/2) else set /a len-=%n%/2
set /a n=%n%/2
call set "temp=%%str:~%len%,1%%"
if defined temp (set /a len+=%n%/2) else set /a len-=%n%/2
set /a n=%n%/2
call set "temp=%%str:~%len%,1%%"
if defined temp (set /a len+=%n%/2) else set /a len-=%n%/2
set /a n=%n%/2
call set "temp=%%str:~%len%,1%%"
if defined temp (set /a len+=%n%/2) else set /a len-=%n%/2
set /a n=%n%/2
call set "temp=%%str:~%len%,1%%"
if defined temp (set /a len+=%n%/2) else set /a len-=%n%/2
set /a n=%n%/2
call set "temp=%%str:~%len%,1%%"
if defined temp (set /a len+=%n%/2) else set /a len-=%n%/2
call set "temp=%%str:~%len%,1%%"
if defined temp (set /a len+=0) else set /a len-=1

endlocal &set "%~2=%len%"
goto :eof




Write a temporary file and read the file size:

Code: Select all

@echo off &setlocal
set var=TestString


call :len "%var%"
echo The length of %var% is %len%.
pause
goto :eof

:len
  >len.tmp echo %~1
  :: get the file size in bytes
  for %%i in (len.tmp) do set /a len=%%~zi
  :: subtract 2 bytes (CR and LF)
  set /a len-=2
  del len.tmp
  goto :eof



Use the offset-option of FINDSTR:

Code: Select all

@echo off &setlocal
set var=TestString

for /f "delims=:" %%i in ('^(echo.%var%^&echo.^)^|findstr /o $') do set /a len=%%i-3
echo %len%
pause>nul



Regards
aGerman