System.String.Split(null) 不删除空格 (C#)
Posted
技术标签:
【中文标题】System.String.Split(null) 不删除空格 (C#)【英文标题】:System.String.Split(null) doesn't remove whitespace (C#) 【发布时间】:2013-06-01 23:55:09 【问题描述】:我知道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 错误的 错误的 错误的 真的 真的 真的 真的 真的 真的 错误的 错误的 错误的 628 (空的) (空的) (空的) (空的) (空的) 5911.3097 (空的) (空的) (空的) (空的) (空的) 1660.0134 (空的) (空的) (空的) (空的) (空的) 3771.8285在我看来,System.String.Split(null)
刚刚为我删除了一个空格:S
我正在使用:Unity3D、Mono、C#、Mac OSX 10.8
【问题讨论】:
Split 返回一个字符串数组,其中包含此实例中由元素分隔的子字符串。所以这里它返回子字符串...找到的每个空格...它不会删除空格... 如果两个分隔符相邻,或者在该实例的开头或结尾找到一个分隔符,则对应的数组元素包含Empty.so它不是空格它为空...... @AmitSingh 谢谢!我已经在我的问题中编辑了输出。 【参考方案1】:我怀疑问题是您将空字符串与空格混淆了。让我演示一下:
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
*/
【讨论】:
以上是关于System.String.Split(null) 不删除空格 (C#)的主要内容,如果未能解决你的问题,请参考以下文章
三个问题: NULL - NULL 定义了吗? (uintptr_t)NULL - (uintptr_t)NULL 定义了吗? [复制]