How to get string length (two possibilites)

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
miskox
Posts: 397
Joined: 28 Jun 2010 03:46

How to get string length (two possibilites)

#1 Post by miskox » 28 Jun 2010 05:09

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

aGerman
Expert
Posts: 4191
Joined: 22 Jan 2010 18:01
Location: Germany

Re: How to get string length (two possibilites)

#2 Post by aGerman » 28 Jun 2010 10:43

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

Post Reply