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!
2 comments
2 Comments
Join the conversation! Your thoughts help the community grow.
Join to leave a comment
Members earn 10 SHARP for a comment, 10 SHARP for a reaction, and 1,000 SHARP for every article they publish.
siva sankareditedPosted Jul 11, 2006, 3:53 AM EDTEdited Nov 23, 2006, 3:37 AM EST
Hello sir,
I have a string which contains a delimeter ':@:'. I want to split that string with the given delimeter because i can't pass more than one char.(or) how to use StringTokenizer? can we pass delimeter as more than one character?
Thanks & Regards
Siva Sankar D
siva sankareditedPosted Jul 11, 2006, 3:53 AM EDTEdited Nov 23, 2006, 3:37 AM EST
Hello sir, I have a string which contains a delimeter ':@:'. I want to split that string with the given delimeter because i can't pass more than one char.(or) how to use StringTokenizer? can we pass delimeter as more than one character? Thanks & Regards Siva Sankar D