Page 1 of 3

Split string to characters

Posted: 28 Mar 2012 04:45
by miltos
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

Re: Split string to characters

Posted: 28 Mar 2012 05:32
by Squashman
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

Posted: 28 Mar 2012 06:26
by foxidrive
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

Posted: 28 Mar 2012 06:37
by Squashman
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

Posted: 28 Mar 2012 07:18
by foxidrive
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

Posted: 28 Mar 2012 07:55
by miltos
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...

Re: Split string to characters

Posted: 28 Mar 2012 10:02
by foxidrive
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"

Re: Split string to characters

Posted: 28 Mar 2012 10:14
by miltos
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 :roll:

anyway, thnx again :D

Re: Split string to characters

Posted: 28 Mar 2012 10:21
by foxidrive
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

Re: Split string to characters

Posted: 28 Mar 2012 11:20
by !k
Words from a phrase into Variables viewtopic.php?p=9814#p9814

Re: Split string to characters

Posted: 28 Mar 2012 11:49
by foxidrive
!k wrote:Words from a phrase into Variables viewtopic.php?p=9814#p9814


That's good. Well done.

Re: Split string to characters

Posted: 28 Mar 2012 12:01
by Aacini
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

Posted: 28 Mar 2012 12:29
by foxidrive
That's neat too.

Re: Split string to characters

Posted: 28 Mar 2012 13:57
by miltos
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

Posted: 29 Mar 2012 00:16
by jeb
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