Rob van der Woude's Scripting Pages

Get String Length

Count the number of characters in a string.

There are several techniques to get the length of a string.
I will list some of these here, each with their pros and cons.

 

As always, enclose a string in doublequotes if it contains any "special characters, e.g. space, tab, ampersand, redirect, pipe, percent, exclamation, comma, caret, etc.
In batch-only solutions, strings are limited to a single line only, not much longer than 8100 characters, and no linefeeds or carriage returns (maximum total command line length is 8192).

 

To recognize line wrapping, command lines are interlaced with blank lines.
So in the following tables, you should treat command lines not separated by blank lines as a single command line.

 

Get String Length
# Code
Pros and Cons
1
>%Temp%\~stringlength.tmp ECHO AbcdEfgh

REM SET /A is used to subtract 2, compensating for the

REM carriage return linefeed appended by the ECHO command

FOR %%A IN (%Temp%\~stringlength.tmp) DO (

	SET /A StringLength = %%~zA - 2

)

DEL %Temp%\~stringlength.tmp

ECHO Length of string AbcdEfgh is %StringLength%
  • 👍 Simple, not much code required
  • 👍 Works with strings of up to 8100+ characters
  • 👍 String length hardly affects speed
  • ✋ Embedding doublequotes are included in the count
  • 👎 A temporary file is created, requires cleanup afterwards
2
SETLOCAL ENABLEDELAYEDEXPANSION

SET String=AbcdEfgh

SET Length=0

FOR /L %%A IN (0,1,8100) DO (

    IF NOT "!String:~%%A,1!"=="" (

        SET /A Length = %%A + 1

    )

)

ECHO Length of string AbcdEfgh is %Length%

ENDLOCAL
  • 👍 Simple, not much code required
  • 👍 Works with strings of up to 8100 characters
  • ✋ Embedding doublequotes are included in the count
  • 👎 Slow
3
CALL :StringLength strlen "AbcdEfgh"

ECHO Length of string "AbcdEfgh" is %strlen%

GOTO:EOF



:StringLength

:: Based on code by jeb on StackOverflow.com

:: https://stackoverflow.com/a/5841587

:: Usage: CALL :StringLength resultvarname string

:: string must be doublequoted if it contains spaces

:: or special characters (e.g. ampersand, redirect,

:: pipe, percent, exclamation, comma, caret).

:: Doublequotes are not counted in length.

SETLOCAL ENABLEDELAYEDEXPANSION

SET string=%~2

IF DEFINED string (

    SET L=1

    FOR %%P IN (4096 2048 1024 512 256 128 64 32 16 8 4 2 1) DO (

        IF "!string:~%%P,1!" NEQ "" (

            SET /A L += %%P

            SET string=!string:~%%P!

        )

    )

) ELSE (

    SET L=0

)

(

    ENDLOCAL

    SET %~1=%L%

    GOTO:EOF

)
  • 👍 Works with strings of up to 8100+ characters
  • 👍 Uses successive approximation, much faster than looping through each individual character
  • 👍 String length hardly affects speed
  • ✋ Embedding doublequotes are ignored in the count
  • 👎 A lot of code
4
SET String=AbcdEfgh

FOR /F "usebackq" %%A IN (`pwsh.exe -c "'%String%'.Length"`) DO (

	SET Length=%%A

)

ECHO Length of string "%String%" is %Length%
  • 👍 Simple, not much code required
  • 👍 Works with strings of up to 8000+ characters
  • 👍 Fast
  • 👍 String length hardly affects speed
  • 👎 Embedding (double)quotes may break the command
  • 👎 Requires PowerShell

page last modified: 2024-07-23; loaded in 0.0024 seconds