Java Like StringTokenizer in C#


The StringTokenizer class is yet another implementation of a java-like StringTokenizer in C#. Full documentation is also included in the zipped .NET Solution. However, internally this one works almost exclusively with Strings, and also takes strings as the arguments (also now takes char[] as delimiter, which is converted to a string). Only 1 delimiter is accepted in an instance, but the code is easily extensible to support more. It was designed as a utility class to tokenize HTTP Requests for another application I am working on. Also, if you plan on tokenizing many strings serially with the same delimiter, you can do it with one instance of the StringTokenizer and just change the source String. Changing the source (or delimiter) string automatically resets the StringTokenizer and re-tokenizes it. Here is an example on how to use it:

//**********************************
string src1 = "The fat cat walked on the wall.";
StringTokenizer st =
new StringTokenizer(src1,"");
//If an empty delimiter is given, automatically uses " " (space).
while(st.HasMoreTokens())
{
Console.WriteLine(st.NextToken());
}
string src2 = "Hello World!";
st.NewSource(src2);
while(st.HasMoreTokens())
{
Console.WriteLine(st.NextToken());
}
//**********************************

The output would be:

The
fat
cat
walked
on
the
wall.
Hello
World!


Similar Articles