467,923 Members | 1,420 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 467,923 developers. It's quick & easy.

Tokenize a string in C#

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
Nov 17 '05 #1
  • viewed: 31031
Share:
2 Replies
James <Ja***@discussions.microsoft.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #2
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] ;
}

"James" <Ja***@discussions.microsoft.com> wrote in message
news:DB**********************************@microsof t.com...
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

Nov 17 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

16 posts views Thread by qwweeeit | last post: by
9 posts views Thread by Lans | last post: by
4 posts views Thread by Kelvin | last post: by
5 posts views Thread by Lam | last post: by
20 posts views Thread by bubunia2000 | last post: by
1 post views Thread by Tim | last post: by
2 posts views Thread by askalottaqs | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.