Split string to characters
Moderator: DosItHelp
Split string to characters
Although I can easily split a string using tokens delims. etc. i cannot split string to characters.
e.g.
set mystring=example
for /f ??????? %%i in ("%mystring%") do (
echo %%i
)
output should be:
e
x
a
m
p
l
e
your help is appreciated
e.g.
set mystring=example
for /f ??????? %%i in ("%mystring%") do (
echo %%i
)
output should be:
e
x
a
m
p
l
e
your help is appreciated
Re: Split string to characters
Get the String Length which I believe we have a function already written for in our Library here and then use a For /L loop and use string parsing to echo each character individually.
Re: Split string to characters
Here is one method:
Code: Select all
@echo off
set "name=example"
set "num=-1"
:loop
set /a num=num+1
call set "name2=%%name:~%num%,1%%"
if defined name2 (
echo(%name2%
goto :loop
)
pause
Re: Split string to characters
foxidrive wrote:Here is one method:Code: Select all
@echo off
set "name=example"
set "num=-1"
:loop
set /a num=num+1
call set "name2=%%name:~%num%,1%%"
if defined name2 (
echo(%name2%
goto :loop
)
pause
I like that. Much simpler than using the string length function and a FOR /L loop.
Re: Split string to characters
Thanks. This echos the word in reverse.
Code: Select all
@echo off
set "name=example"
set "num=-1"
:countlength
set /a num=num+1
call set "name2=%%name:~%num%,1%%"
if defined name2 goto :countlength
:loop
set /a num=num-1
call set "name2=%%name:~%num%,1%%"
if %num% GTR -1 (
echo(%name2%
goto :loop
)
pause
Re: Split string to characters
Thanks; works like a charm!
Just a question: why do I need call before set?
I understand the script but i don't get how set and double quotation marks success running the script.
I'm trying without call but i get errors
What am I doing wrong?
set name2=%name:~%num%,1%
Just to satisfy my curiosity...
Thnx again, because google didn't help me a lot there...
Just a question: why do I need call before set?
I understand the script but i don't get how set and double quotation marks success running the script.
I'm trying without call but i get errors
What am I doing wrong?
set name2=%name:~%num%,1%
Just to satisfy my curiosity...
Thnx again, because google didn't help me a lot there...
Last edited by miltos on 28 Mar 2012 15:37, edited 1 time in total.
Re: Split string to characters
Using call runs an extra CMD processor, and then the doubled percents are reduced to single percents. The %num% variable in the line replaced with the value.
This allows you to use a variable where a variable is not designed to be used.
This is one of the undocumented techniques used in batch files.
EDIT: I used the wrong word "quotes" vs "percents"
This allows you to use a variable where a variable is not designed to be used.
This is one of the undocumented techniques used in batch files.
EDIT: I used the wrong word "quotes" vs "percents"
Last edited by foxidrive on 28 Mar 2012 10:24, edited 1 time in total.
Re: Split string to characters
foxidrive wrote:Using call runs an extra CMD processor, and then the doubled quotes are reduced to single quotes. The %num% variable in the line replaced with the value.
This allows you to use a variable where a variable is not designed to be used.
This is one of the undocumented techniques used in batch files.
although
set name2=%name:~0,1%
works, the following
set name2=%name:~%num%,1%
doesn't
and
call set "name2=%%name:~%num%,1%%"
works
sometimes I cannot understand microsoft
anyway, thnx again
Re: Split string to characters
It's undocumented. Don't blame Microsoft.
Take this example:
call set "name2=%%name:~%num%,1%%"
If name=abc and num=2
when you call it the command that is executed is this:
set "name2=%name:~2,1%"
because the two consecutive percent signs are reduced to one percent sign by the extra command processor that is invoked by using the CALL keyword.
At the same time %num% is replaced with the value of 2
So name2=c
Take this example:
call set "name2=%%name:~%num%,1%%"
If name=abc and num=2
when you call it the command that is executed is this:
set "name2=%name:~2,1%"
because the two consecutive percent signs are reduced to one percent sign by the extra command processor that is invoked by using the CALL keyword.
At the same time %num% is replaced with the value of 2
So name2=c
Re: Split string to characters
Words from a phrase into Variables viewtopic.php?p=9814#p9814
Re: Split string to characters
This is another, simpler method:
Code: Select all
@echo off
set mystring=example
:loop
if defined mystring (
echo(%mystring:~0,1%
set "mystring=%mystring:~1%"
goto loop
)
Re: Split string to characters
That's neat too.
Re: Split string to characters
Aacini wrote:This is another, simpler method:Code: Select all
@echo off
set mystring=example
:loop
if defined mystring (
echo(%mystring:~0,1%
set "mystring=%mystring:~1%"
goto loop
)
good
Re: Split string to characters
foxidrive wrote:Using call runs an extra CMD processor, and then the doubled percents are reduced to single percents. The %num% variable in the line replaced with the value.
This allows you to use a variable where a variable is not designed to be used.
No, CALL does not run an extra CMD processor, it only restarts a new parser loop (and stops after the special character phase).
Call has also some side effects, it's very slow and it doubles all carets, for more you could read
CALL me, or better avoid call
jeb