C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Communities   Interview   Jobs   Projects   Offshore Development    
Silverlight Tutorials | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Revenue Sharing |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...

New Feature: Community Sites: Create your own .NET community website and start earning from Google AdSense ! It's Free !




StringTokenizer using C#


Posted Date: 15 Mar 2007    Resource Type: Code Snippets    Category: String Manipulations

Posted By: Prajnan Das       Member Level: Gold
Rating:     Points: 10



This is a simple implementation of a String Tokenizer in C#. The target string to be tokenized and the tokens as char array can be used to create an instance of String Tokenizer. Once created it can be iterated over and over again using the foreach loop. It also demonstrates the usage of the IEnumerator interface to iterate through the internal array of strings, which is formed by spilting the target string only once using the string method spilt.

StringTokenizer class implements the interfaces IEnumerable, IEnumerator. An object of StringTokenizer can be created using the target string and tokens to used in the form a char array.

public class StringTokenizer : IEnumerable, IEnumerator
{
string[] tokensizedStrs;
int index = -1;
public StringTokenizer(string target, char[] tokens)
{
tokensizedStrs = target.Split(tokens);
}
public IEnumerator GetEnumerator()
{
return this;
}
object IEnumerator.Current
{
get
{
return tokensizedStrs[index];
}
}
bool IEnumerator.MoveNext()
{
if (++index >= tokensizedStrs.Length)
return false;
else
return true;
}
void IEnumerator.Reset()
{
index = -1;
}
}

Here is a test code snippet:

StringTokenizer strTokenizer = new StringTokenizer(".NET is less complex than J2EE", new char[] { ' ' });
foreach (string strTokenized in strTokenizer)
Console.WriteLine(strTokenized);




Responses


No responses found. Be the first to respond and make money from revenue sharing program.

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
(No tags found.)

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Convert a string into Title Case or ProperCase
Previous Resource: Proper case validation
Return to Discussion Resource Index
Post New Resource
Category: String Manipulations


Post resources and earn money!
 
Related Resources



dotNet Slackers   BizTalk Adaptors    Web Design

UK Conference calling Company

Contact Us    Privacy Policy    Terms Of Use