Reputation: 61
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?
Upvotes: 1
Views: 3805
Reputation: 628
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%
.
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 +1
using %Log:0=+1%
and evaluate the resulting expression using set /a
, giving us our final result.
Upvotes: 2
Reputation: 70943
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%"
Upvotes: 0
Reputation: 9545
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
Upvotes: 0
Reputation: 9816
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?
Upvotes: 0