文字列の分割

文字列の分割の練習です。

using System;
using System.Text;

class StringSplit {
    static void P(string[] ss) {
        var sb = new StringBuilder();
        sb.Append("[");
        for (int i = 0; i < ss.Length; i++) {
            if (i > 0) sb.Append(",");
            sb.Append(ss[i]);
        }
        sb.Append("]");
        Console.WriteLine(sb);
    }

    static void Main() {
        var s = "hello world";
        P(s.Split()); // [hello,world]

        s = "hello  world";
        P(s.Split()); // [hello,,world]

        s = " hello\tworld";
        P(s.Split()); // [,hello,world]

        s = " hello world\n";
        P(s.Split()); // [,hello,world,]

        s = "  hello    world";
        P(s.Split(new char[]{ ' ' }, StringSplitOptions.RemoveEmptyEntries));
        // [hello,world]
    }
}

参考: