我知道System.String.Split(null)
应该返回一个删除了空格的字符串数组。我读过this post和this 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 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
我正在使用:Unity3D,Mono,C#,Mac OSX 10.8
答案 0 :(得分:5)
我怀疑问题是你把空字符串与空格混淆了。让我演示一下:
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
*/
}