tags:

views:

15078

answers:

9

I'm doing simple string input parsing and I am in need of a string tokenizer. I am new to C# but have programmed Java, and it seems natural that C# should have a string tokenizer. Does it? Where is it? How do I use it?

+4  A: 

I think the nearest in the .NET Framework is

string.Split()
Steve Morgan
A: 

I don't know Java, but i think you need String.Split.

Biri
+9  A: 

The split method of a string is what you need. In fact the tokenizer class in Java is depreciated in favor of Java's string split method as well.

Tim Jarvis
+28  A: 

You could use String.Split method.

class ExampleClass
{
    public ExampleClass()
    {
        string exampleString = "there is a cat";
        // Split string on spaces. This will separate all the words in a string
        string[] words = exampleString.Split(' ');
        foreach (string word in words)
        {
            Console.WriteLine(word);
            // there
            // is
            // a
            // cat
        }
    }
}

For more information see Sam Allen's article about splitting strings in c# (Performance, Regex)

Davy Landman
+1  A: 

For complex splitting you could use a regex creating a match collection.

A: 

If you are using C# 3.5 you could write an extension method to System.String that does the splitting you need. You then can then use syntax:

string.SplitByMyTokens();

More info and a useful example from MS here http://msdn.microsoft.com/en-us/library/bb383977.aspx

Paul Shannon
This is a solution to a local problem, not an obvious/general purpose System.String operation. A utility class might be in order, but it would be extension method abuse to use an extension method here.
280Z28
A: 

what about splitting on something like test123#|#test456#|#test789, if I want to split on the #|# String sequence

have you tried the String.Split ? it has a overload which accepts a Strings as the token to split on.
Davy Landman
+1  A: 

use Regex.Split(string,"#|#");

A: 

read this, split function has an overload takes an array consist of seperators http://msdn.microsoft.com/en-us/library/system.stringsplitoptions.aspx

Musa