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

2019-01-09 00:04发布

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?

11条回答
Ridiculous、
2楼-- · 2019-01-09 00:37
_words = new List<string>(YourText.ToLower().Trim('\n', '\r').Split(' ').
            Select(x => new string(x.Where(Char.IsLetter).ToArray()))); 

Or

_words = new List<string>(YourText.Trim('\n', '\r').Split(' ').
            Select(x => new string(x.Where(Char.IsLetterOrDigit).ToArray()))); 
查看更多
Fickle 薄情
3楼-- · 2019-01-09 00:40

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
查看更多
聊天终结者
4楼-- · 2019-01-09 00:45

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

查看更多
我欲成王,谁敢阻挡
5楼-- · 2019-01-09 00:45

If you're trying to do something like splitting command line arguments in a .NET Console app, you're going to have issues because .NET is either broken or is trying to be clever (which means it's as good as broken). I needed to be able to split arguments by the space character, preserving any literals that were quoted so they didn't get split in the middle. This is the code I wrote to do the job:

private static List<String> Tokenise(string value, char seperator)
{
    List<string> result = new List<string>();
    value = value.Replace("  ", " ").Replace("  ", " ").Trim();
    StringBuilder sb = new StringBuilder();
    bool insideQuote = false;
    foreach(char c in value.ToCharArray())
    {
        if(c == '"')
        {
            insideQuote = !insideQuote;
        }
        if((c == seperator) && !insideQuote)
        {
            if (sb.ToString().Trim().Length > 0)
            {
                result.Add(sb.ToString().Trim());
                sb.Clear();
            }
        }
        else
        {
            sb.Append(c);
        }
    }
    if (sb.ToString().Trim().Length > 0)
    {
        result.Add(sb.ToString().Trim());
    }

    return result;
}
查看更多
狗以群分
6楼-- · 2019-01-09 00:52

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

查看更多
Summer. ? 凉城
7楼-- · 2019-01-09 00:55

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

查看更多
登录 后发表回答