Counting string length

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Velpreso
Posts: 18
Joined: 28 Dec 2012 10:03

Counting string length

#1 Post by Velpreso » 11 Feb 2013 14:46

Checking long of word. I am doing something wrong because I must admit I do not understand in 100% how it works. I would like to enter a word and Batch will show its length so I converted that script from library:

Code: Select all

(   SETLOCAL ENABLEDELAYEDEXPANSION
    set "str=A!%~1!"&rem keep the A up front to ensure we get the length and not the upper bound
                     rem it also avoids trouble in case of empty string
    set "len=0"
    for /L %%A in (12,-1,0) do (
        set /a "len|=1<<%%A"
        for %%B in (!len!) do if "!str:~%%B,1!"=="" set /a "len&=~1<<%%A"
    )
)
( ENDLOCAL & REM RETURN VALUES
    IF "%~2" NEQ "" SET /a %~2=%len%
)
EXIT /b


to:

Code: Select all

@echo off
set /p str=
(   SETLOCAL ENABLEDELAYEDEXPANSION
    set "str=A!%~1!"
                     
    set "len=1"
    for /L %%A in (12,-1,0) do (
        set /a "len|=1<<%%A"
        for %%B in (!len!) do if "!str:~%%B,1!"=="" set /a "len&=~1<<%%A"
    )
)
( ENDLOCAL
    IF "%~2" NEQ "" SET /a %~2=%len%
)
echo Show: %len%
pause > nul


but doesn't work, how to fix it ?

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: How to make it ?

#2 Post by Squashman » 11 Feb 2013 15:18

Your script from your other thread plus the string length function

Code: Select all

@echo off

set str=TEST
set string1=%str:~0,2%
echo %string1%

CALL :strLen string1 len

echo Length is %len%
pause

GOTO :EOF

:strLen string len -- returns the length of a string
::                 -- string [in]  - variable name containing the string being measured for length
::                 -- len    [out] - variable to be used to return the string length
:: Many thanks to 'sowgtsoi', but also 'jeb' and 'amel27' dostips forum users helped making this short and efficient
:$created 20081122 :$changed 20101116 :$categories StringOperation
:$source http://www.dostips.com
(   SETLOCAL ENABLEDELAYEDEXPANSION
    set "str=A!%~1!"&rem keep the A up front to ensure we get the length and not the upper bound
                     rem it also avoids trouble in case of empty string
    set "len=0"
    for /L %%A in (12,-1,0) do (
        set /a "len|=1<<%%A"
        for %%B in (!len!) do if "!str:~%%B,1!"=="" set /a "len&=~1<<%%A"
    )
)
( ENDLOCAL & REM RETURN VALUES
    IF "%~2" NEQ "" SET /a %~2=%len%
)
EXIT /b

shirulkar
Posts: 40
Joined: 15 Jan 2013 23:53

Re: How to make it ?

#3 Post by shirulkar » 11 Feb 2013 17:07

Hi,
Following code will work but only for word without space.

Code: Select all

@echo off
SETLOCAL ENABLEDELAYEDEXPANSION
set /p str= Enter word:
set len=0
FOR %%a in (%str%) do ( set str3=%%a
call :fun1 %%a)

Exit /b

:fun1
REM set str1=%~1
REM set a=%str1%
set str2=!str3:~%len%,1!
if "%str2%"=="" ( call :endof ) ELSE (
set /a len=%len%+1
call :fun1 %str3% )

Exit /b

:endof
set /a len=%len%-1
echo %len%

Exit /b

shirulkar
Posts: 40
Joined: 15 Jan 2013 23:53

Re: How to make it ?

#4 Post by shirulkar » 11 Feb 2013 17:30

Following code will give the length of the word. If it contain space then also it will give the length of whole string.

Ex. I am good
then it will give length as 9.

Code: Select all

@echo off
SETLOCAL ENABLEDELAYEDEXPANSION
set /p str= Enter word:
set len=0
set flen=0
FOR /f "usebackq delims=" %%a in (` echo %str%`) do ( set "str3=%%a"
call :fun1 %%a)

Exit /b

:fun1
REM set str1=%~1
REM set a=%str1%
set str2=!str3:~%len%,1!
if "%str2%"=="" ( set /a flen=!flen!+!len!
echo !flen!
call :endof
) ELSE (
set /a len=%len%+1
call :fun1 %str3% )

Exit /b

:endof
set /a len=%len%-1
REM echo %len%

Exit /b

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: How to make it ?

#5 Post by Squashman » 11 Feb 2013 20:45

Code: Select all

Enter word:!myline%%
8

Last time I checked that equals 9.

Velpreso
Posts: 18
Joined: 28 Dec 2012 10:03

Re: Counting string length

#6 Post by Velpreso » 12 Feb 2013 02:38

Thanks for all the posts and fix my script. It works as it should. You are great: Squashman and shirulkar. Thanks for your help.

Post Reply