System.String.Split(null)不会删除空格(C#) [英] System.String.Split(null) doesn't remove whitespace (C#)

查看:127
本文介绍了System.String.Split(null)不会删除空格(C#)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道System.String.Split(null)应该返回删除了空格的字符串数组.我已经阅读了这篇文章

I know that System.String.Split(null) should return me a string array with whitespace removed. I've read this post and this MSDN doc, which does not agree with what I'm experiencing.

这是我的代码:

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
False
False
False
True
True
True
True
True
True
False
False
False
628
(empty)
(empty)
(empty)
(empty)
(empty)
5911.3097
(empty)
(empty)
(empty)
(empty)
(empty)
1660.0134
(empty)
(empty)
(empty)
(empty)
(empty)
3771.8285

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

It seems to me that System.String.Split(null) just removed one space for me :S

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

I'm using: Unity3D, Mono, C#, Mac OSX 10.8

推荐答案

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

I suspect the problem is you're confusing an empty string with a space. Let me demonstrate:

    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调用吗?

In future may I suggest you using the following API call?

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
    */
}

这篇关于System.String.Split(null)不会删除空格(C#)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆