Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to loop through tokens in a string?

Tags:

batch-file

Say I have a string such as foo:bar:baz, is it possible to loop through this string? It looked like you could tokenize lines of a file but the following will only echo 'foo' once.

for /f "tokens=1,2* delims=:" %%x in ("%%j") do echo %%x

like image 705
Joe Cartano Avatar asked Dec 13 '11 17:12

Joe Cartano


People also ask

How do you loop through a string?

For loops are used when you know you want to visit every character. For loops with strings usually start at 0 and use the string's length() for the ending condition to step through the string character by character. String s = "example"; // loop through the string from 0 to length for(int i=0; i < s.

Can you loop through a string in Javascript?

Use the string index number to loop through a string In this loop, the variable i automatically receives the index so that each character can be accessed using str[i] .

How do I use string tokens?

To use String Tokenizer class we have to specify an input string and a string that contains delimiters. Delimiters are the characters that separate tokens. Each character in the delimiter string is considered a valid delimiter. Default delimiters are whitespaces, new line, space, and tab.


1 Answers

set string=foo:bar:baz
for %%x in (%string::= %) do echo %%x

FOR value delimiters may be space, comma, semicolon and equal-sign. You may directly process a string if the elements are delimited with any of these characters. If not, just change the delimiter for one of these character (as I did above).

set string=foo bar,baz;one=two
for %%x in (%string%) do echo %%x
like image 82
Aacini Avatar answered Oct 03 '22 20:10

Aacini