Split string to characters

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
miltos
Posts: 6
Joined: 28 Mar 2012 04:38

Split string to characters

#1 Post by miltos » 28 Mar 2012 04:45

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

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: Split string to characters

#2 Post by Squashman » 28 Mar 2012 05:32

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.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Split string to characters

#3 Post by foxidrive » 28 Mar 2012 06:26

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

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: Split string to characters

#4 Post by Squashman » 28 Mar 2012 06:37

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.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Split string to characters

#5 Post by foxidrive » 28 Mar 2012 07:18

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

miltos
Posts: 6
Joined: 28 Mar 2012 04:38

Re: Split string to characters

#6 Post by miltos » 28 Mar 2012 07:55

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...
Last edited by miltos on 28 Mar 2012 15:37, edited 1 time in total.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Split string to characters

#7 Post by foxidrive » 28 Mar 2012 10:02

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"
Last edited by foxidrive on 28 Mar 2012 10:24, edited 1 time in total.

miltos
Posts: 6
Joined: 28 Mar 2012 04:38

Re: Split string to characters

#8 Post by miltos » 28 Mar 2012 10:14

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

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Split string to characters

#9 Post by foxidrive » 28 Mar 2012 10:21

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

!k
Expert
Posts: 378
Joined: 17 Oct 2009 08:30
Location: Russia

Re: Split string to characters

#10 Post by !k » 28 Mar 2012 11:20

Words from a phrase into Variables viewtopic.php?p=9814#p9814

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Split string to characters

#11 Post by foxidrive » 28 Mar 2012 11:49

!k wrote:Words from a phrase into Variables viewtopic.php?p=9814#p9814


That's good. Well done.

Aacini
Expert
Posts: 1914
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: Split string to characters

#12 Post by Aacini » 28 Mar 2012 12:01

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
)

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Split string to characters

#13 Post by foxidrive » 28 Mar 2012 12:29

That's neat too.

miltos
Posts: 6
Joined: 28 Mar 2012 04:38

Re: Split string to characters

#14 Post by miltos » 28 Mar 2012 13:57

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

jeb
Expert
Posts: 1055
Joined: 30 Aug 2007 08:05
Location: Germany, Bochum

Re: Split string to characters

#15 Post by jeb » 29 Mar 2012 00:16

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

Post Reply