1

I want to find a way to know the number of digits in variable. For now, I'm trying to use this code. In this example, %var% is the variable that I need to know the number of digits it has.

    set x=1
    set var=12345
    :LOOP
    set temp=%var:~0,%x%%
    if %temp%==%var% goto END
    set x=%x%+1
    goto LOOP
    :END

Theoretically, at the end of the code %x% would be the number of digits %var% has. However, it doesn't work. I found out the problem is at the 3rd line. I modified the code to diagnose:

    set x=1
    set var=12345
    :LOOP
    set temp=%var:~0,%x%%
    echo %temp%
    pause
    if %temp%==%var% goto END
    set x=%x%+1
    goto LOOP
    :END

The result echoed was:

    x%%

Can anyone pinpoint my mistake or give an alternative solution to determine the number of digits in a variable?

heyzec
  • 61
  • 1
  • 6
  • What do you mean by "number of digits?" Will your test strings be all-digits? will there be a size limit? Do you mean "distinct digits" (so 121212 has 2 distinct digits) or are you interested in the number of numerical characters (so 'hello123' would have 3?) – Magoo Apr 07 '14 at 15:50
  • Yes, all variable would be numbers. 12345 would have 5 digits. There's no size limit. – heyzec Apr 08 '14 at 11:59
  • I suggest you to review [Reinstate Monica](https://stackoverflow.com/a/45472269/778560)'s answer below... – Aacini Jun 12 '22 at 19:37

4 Answers4

2

Here's a short solution, that only works for numeric variables:

set /a Log=1%var:~1%-%var:~1% -0
set /a Len=%Log:0=+1%

The variable %Len% will contain the number of digits in %var%.

Explanation

The basic idea is to convert the first digit to 1, and the rest of them (the 'trailing' digits) to 0's. Then we can use the string replacement function to replace all the 0's with +1 giving us 1+1+1.... and evaluate the string as an arithmetic expression. This will give us the total number of digits.

The 'trailing' digits can be gotten using %var:~1% and we convert them to 0 by subtracting them from the variable itself: 45678 - 5678 gives 40000 etc. However, the above code subtracts them from 1%var:~1% instead, in order to replace the first digit with 1 (i.e. 1 followed by the 'trailing' digits).

The reason for the extra -0 is in case %var% only has one digit, for example 7. In that case, the expression 1%var:~1%-%var:~1% would evaluate to 1- and the shell would complain: Missing operand. The -0 ensures that we always have a valid expression.

Now that we've converted the variable in to the proper form into %Log%, we can replace every occurrence of 0 with +1using %Log:0=+1% and evaluate the resulting expression using set /a, giving us our final result.

Reinstate Monica
  • 588
  • 7
  • 21
  • 1
    This method is fantastic! **`;)`** You may simplify it a little this way: `set /a Log=1%var%-var` and `set /a Len=%Log:0=+1% -1` – Aacini Jun 12 '22 at 19:01
  • @Aacini Thanks! Feel free to edit it in at the end of the answer. I'll leave the original as is because I can't be bothered to modify the explanation, and because the `+0` has pedagogical value. – Reinstate Monica Jun 12 '22 at 21:13
0

As there is no build in function for string length, you can write your own function.

@echo off
setlocal
set "myString=abcdef!%%^^()^!"
call :strlen result myString
echo %result%
goto :eof

:strlen <resultVar> <stringVar>
(   
    setlocal EnableDelayedExpansion
    set "s=!%~2!#"
    set "len=0"
    for %%P in (4096 2048 1024 512 256 128 64 32 16 8 4 2 1) do (
        if "!s:~%%P,1!" NEQ "" ( 
            set /a "len+=%%P"
            set "s=!s:~%%P!"
        )
    )
)
( 
    endlocal
    set "%~1=%len%"
    exit /b
)

This function needs always 13 loops, instead of a simple strlen function which needs strlen-loops.
It handles all characters.

Source: How do you get the string length in a batch file?

Community
  • 1
  • 1
09stephenb
  • 9,358
  • 15
  • 53
  • 91
  • Okay... quite a long code, but definitely usable. Can anyone pinpoint my code's mistake? – heyzec Apr 07 '14 at 13:19
  • It isn't much of a mistake its that doing this is surprising complicated. Look at @MCND Answer it shows you more about your code and how to improve it. – 09stephenb Apr 08 '14 at 08:21
0

Your are trying to do this loop :

@Echo Off
Set /P VrStr=Enter your string :
:Loop
If "%VrStr%" EQU "" Goto EndLoop
Set VrStr=%VrStr:~0,-1%
Set /A VrLgr+=1
Goto Loop
:EndLoop
Echo Number of char: %VrLgr%
Pause

You can use this to :

@echo off
setlocal EnableDelayedExpansion
Set /P $Tstring=Enter your string:
for /l %%a in (0,1,9000) do (set $t=!$Tstring:~%%a,1!&if not defined $t (echo [NB OF CHAR =] %%a&pause&exit /b))
pause
SachaDee
  • 9,245
  • 3
  • 23
  • 33
0

The main problem with your code is

 set temp=%var:~0,%x%%

This does not work. The parser is not able to properly determine what percent sign belongs to what variable. You can enable delayed expansion and write it as

set "temp=!var:~0,%x%!"

For alternative versions, to handle any length string, any of the posted answers will work.

For a simpler solution, if you are sure the string is under 10 characters, then this is an alternative

set "x=0123456789%var%"
set "x=%x:~-10,1%"
MC ND
  • 69,615
  • 8
  • 84
  • 126