Does C# have a String Tokenizer like Java's?

前端 未结 11 1496
日久生厌
日久生厌 2020-12-01 11:31

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 tok

相关标签:
11条回答
  • 2020-12-01 12:07

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

    0 讨论(0)
  • 2020-12-01 12:08

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

    0 讨论(0)
  • 2020-12-01 12:12

    I think the nearest in the .NET Framework is

    string.Split()
    
    0 讨论(0)
  • 2020-12-01 12:13

    The similar to Java's method is:

    Regex.Split(string, pattern);
    

    where

    • string - the text you need to split
    • pattern - string type pattern, what is splitting the text
    0 讨论(0)
  • 2020-12-01 12:14

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

    0 讨论(0)
  • 2020-12-01 12:21

    I just want to highlight the power of C#'s Split method and give a more detailed comparison, particularly from someone who comes from a Java background.

    Whereas StringTokenizer in Java only allows a single delimiter, we can actually split on multiple delimiters making regular expressions less necessary (although if one needs regex, use regex by all means!) Take for example this:

    str.Split(new char[] { ' ', '.', '?' })
    

    This splits on three different delimiters returning an array of tokens. We can also remove empty arrays with what would be a second parameter for the above example:

    str.Split(new char[] { ' ', '.', '?' }, StringSplitOptions.RemoveEmptyEntries)
    

    One thing Java's String tokenizer does have that I believe C# is lacking (at least Java 7 has this feature) is the ability to keep the delimiter(s) as tokens. C#'s Split will discard the tokens. This could be important in say some NLP applications, but for more general purpose applications this might not be a problem.

    0 讨论(0)
提交回复
热议问题