现在的位置: 首页 > 综合 > 正文

<笔记>如何分割string

2014年03月11日 ⁄ 综合 ⁄ 共 522字 ⁄ 字号 评论关闭
class TestStringSplit
{
    static void Main()
    {
        char[] delimiterChars = { ' ', ',', '.', ':', '\t' };

        string text = "a\tb c:d,e f g";
        System.Console.WriteLine("Original text: '{0}'", text);

        string[] words = text.Split(delimiterChars);
        System.Console.WriteLine("{0} words in text:", words.Length);

        foreach (string s in words)
        {
            System.Console.WriteLine(s);
        }

        // Keep the console window open in debug mode.
        System.Console.WriteLine("Press any key to exit.");
        System.Console.ReadKey();
    }
}
/* Output:
    a
    b
    c
    d
    e
    f
    g
 */

As input, Split takes an array of chars that indicate which characters are to be used as delimiters.

【上篇】
【下篇】

抱歉!评论已关闭.