Page 1 of 1

Counting string length

Posted: 11 Feb 2013 14:46
by Velpreso
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 ?

Re: How to make it ?

Posted: 11 Feb 2013 15:18
by Squashman
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

Re: How to make it ?

Posted: 11 Feb 2013 17:07
by shirulkar
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

Re: How to make it ?

Posted: 11 Feb 2013 17:30
by shirulkar
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

Re: How to make it ?

Posted: 11 Feb 2013 20:45
by Squashman

Code: Select all

Enter word:!myline%%
8

Last time I checked that equals 9.

Re: Counting string length

Posted: 12 Feb 2013 02:38
by Velpreso
Thanks for all the posts and fix my script. It works as it should. You are great: Squashman and shirulkar. Thanks for your help.