SIGN UP MEMBER LOGIN:    
ARTICLE

Java Like StringTokenizer in C#

Posted by paul Articles | String in C# June 11, 2003
The StringTokenizer class is yet another implementation of a java-like StringTokenizer in C#.
Reader Level:
Download Files:
 

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!

Login to add your contents and source code to this article
share this article :
post comment
 

U can use regular expressions to split strings having multiple delimiters

Posted by athi appan Jan 28, 2009

 

 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

Posted by siva sankar Jul 11, 2006
Nevron Gauge for SharePoint
Become a Sponsor
PREMIUM SPONSORS
  • The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
    Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
Become a Sponsor