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
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.
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] .
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With