gpt4 book ai didi

c# - System.String.Split(null) 不删除空格 (C#)

转载 作者:行者123 更新时间:2023-11-30 14:59:20 25 4
gpt4 key购买 nike

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

这是我的代码:

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              0FalseFalseFalseTrueTrueTrueTrueTrueTrueFalseFalseFalse628(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

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

最佳答案

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

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

关于c# - System.String.Split(null) 不删除空格 (C#),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16954216/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com