3

我知道这System.String.Split(null)应该返回给我一个删除了空格的字符串数组。我读过这篇文章这个 MSDN 文档,这与我所经历的不符。

这是我的代码:

void MyFunction(string info)
{
    print(info);
    print(Char.IsWhiteSpace(info,0));
    print(Char.IsWhiteSpace(info,1));
    print(Char.IsWhiteSpace(info,2));
    print(Char.IsWhiteSpace(info,3));
    print(Char.IsWhiteSpace(info,4));
    print(Char.IsWhiteSpace(info,5));
    print(Char.IsWhiteSpace(info,6));
    print(Char.IsWhiteSpace(info,7));
    print(Char.IsWhiteSpace(info,8));
    print(Char.IsWhiteSpace(info,9));
    print(Char.IsWhiteSpace(info,10));
    print(Char.IsWhiteSpace(info,11));

    string [] split = info.Split();
    foreach(string s in split)
        print(s);
}

这是输出:

628 5911.3097 1660.0134 3771.8285 0
错误的
错误的
错误的
真的
真的
真的
真的
真的
真的
错误的
错误的
错误的
628
(空的)
(空的)
(空的)
(空的)
(空的)
5911.3097
(空的)
(空的)
(空的)
(空的)
(空的)
1660.0134
(空的)
(空的)
(空的)
(空的)
(空的)
3771.8285

在我看来,System.String.Split(null)刚刚为我删除了一个空格:S

我正在使用:Unity3D、Mono、C#、Mac OSX 10.8

4

1 回答 1

6

我怀疑问题是您将空字符串与空格混淆了。让我演示一下:

    static void Main(string[] args)
    {
        var info = "628      5911.3097      1660.0134      3771.8285              0";
        Console.WriteLine(info);
        //foreach (var c in info)
        //    Console.WriteLine(Char.IsWhiteSpace(c));

        Console.WriteLine();

        string[] split = info.Split();
        foreach (string s in split)
            Console.WriteLine("\"" + s + "\" is empty: " + (s.Length == 0));

        //What happens if we concat the strings?
        Console.WriteLine();
        Console.WriteLine(string.Concat(split));

        Console.ReadLine();

        /*
        628      5911.3097      1660.0134      3771.8285              0

        "628" is empty: False
        "" is empty: True
        "" is empty: True
        "" is empty: True
        "" is empty: True
        "" is empty: True
        "5911.3097" is empty: False
        "" is empty: True
        "" is empty: True
        "" is empty: True
        "" is empty: True
        "" is empty: True
        "1660.0134" is empty: False
        "" is empty: True
        "" is empty: True
        "" is empty: True
        "" is empty: True
        "" is empty: True
        "3771.8285" is empty: False
        "" is empty: True
        "" is empty: True
        "" is empty: True
        "" is empty: True
        "" is empty: True
        "" is empty: True
        "" is empty: True
        "" is empty: True
        "" is empty: True
        "" is empty: True
        "" is empty: True
        "" is empty: True
        "" is empty: True
        "0" is empty: False

        6285911.30971660.01343771.82850
        */
    }

将来我可以建议您使用以下 API 调用吗?

string[] split = info.Split((char[])null,StringSplitOptions.RemoveEmptyEntries);

像这样:

static void Main(string[] args)
{
    var info = "628      5911.3097      1660.0134      3771.8285              0";
    Console.WriteLine(info);
    Console.WriteLine();

    string[] split = info.Split((char[])null,StringSplitOptions.RemoveEmptyEntries);
    foreach (string s in split)
        Console.WriteLine("\"" + s + "\" is empty: " + (s.Length == 0));

    //What happens if we concat the strings?
    Console.WriteLine();
    Console.WriteLine(string.Concat(split));

    Console.ReadLine();

    /*
    628      5911.3097      1660.0134      3771.8285              0

    "628" is empty: False
    "5911.3097" is empty: False
    "1660.0134" is empty: False
    "3771.8285" is empty: False
    "0" is empty: False

    6285911.30971660.01343771.82850
    */
}
于 2013-06-06T05:37:55.323 回答