Tokenize a string in C#

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi,

I am looking for a stringtokenizer class/method in C#, but can't find one.
The similar classes in Java and C++ are StringTokenizer and
CStringT::tokenize respectively. I need to keep a current position within the
string and change the delimiters dynamically when going throught the string.
Does anyone know a stringtokenizer in c#?

Thanks a lot,

James
 
James said:
I am looking for a stringtokenizer class/method in C#, but can't find one.
The similar classes in Java and C++ are StringTokenizer and
CStringT::tokenize respectively. I need to keep a current position within the
string and change the delimiters dynamically when going throught the string.
Does anyone know a stringtokenizer in c#?

I don't believe there is one - most cases are handled simply using
String.Split, but obviously that doesn't let you change delimiters
dynamically.

Personally I've always found it hard to predict *exactly* what happens
when changing delimiters with Java's StringTokenizer, but maybe that's
just me.

It shouldn't be too hard to write your own one though.
 
Maybe something along the lines of:

string NextToken (out string Tail, string Tokens, params char[] Separator)
{return NextToken (out Tail, Tokens, true, Separator) ;}
string NextToken (out string Tail, string Tokens, bool Trim, params char[]
Separator)
{
Tail = null ;
if (Tokens == null) return null ;
string [] Pieces = Tokens.Split (Separator, 2) ;
if (Pieces.Length > 1) Tail = Trim ? Pieces[1].Trim () : Pieces[1] ;
return Trim ? Pieces[0].Trim () : Pieces[0] ;
}
 
Back
Top