
This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
Implementing a Poor Man’s Tokenizer to Deconstruct a String
|
47
This code moves the startPos pointer beyond the first matched string, disallowing
any internal matches.
To convert this code to use a
while loop rather than a do loop, the foundPos variable
must be initialized to
0, and the while loop expression should be as follows:
while (foundPos >= 0 && startPos < searchStr.Length)
{
foundPos = searchedStr.IndexOf(matchChar, startPos);
if (foundPos > -1)
{
startPos = foundPos + 1;
count++;
}
}
See Also
See the “String.IndexOf Method” and “String.IndexOfAny Method” topics in the
MSDN documentation.
2.6 Implementing a Poor Man’s Tokenizer
to Deconstruct a String
Problem
You need a quick method of breaking up a string into a series of discrete tokens or
words.
Solution
Use the Split instance method of the String class. For example:
string equation = "1 + 2 - 4 * 5";
string[] equationTokens = equation.Split(new char[1]{' '});
foreach (string Tok in equationTokens)
Console.WriteLine(Tok);
This code produces the following output:
1
+
2
-
4
*
5