C# StringSplitOptions.RemoveEmptyEntries 不像宣传的那样工作
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10682301/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
StringSplitOptions.RemoveEmptyEntries doesn't work as advertised
提问by Ben Foster
I've come across this several times in the past and have finally decided to find out why.
我过去曾多次遇到过这种情况,最终决定找出原因。
StringSplitOptions.RemoveEmptyEntrieswould suggest that it removes empty entries.
StringSplitOptions.RemoveEmptyEntries会建议它删除空条目。
So why does this test fail?
那么为什么这个测试会失败呢?
var tags = "One, Two, , Three, Foo Bar, , Day , ";
var tagsSplit = tags.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries)
.Select(s => s.Trim());
tagsSplit.ShouldEqual(new string[] {
"One",
"Two",
"Three",
"Foo Bar",
"Day"
});
The result:
结果:
Values differ at index [2]
Expected string length 5 but was 0. Strings differ at index 0.
Expected: "Three"
But was: <string.Empty>
So it fails because instead of "Three", we have an empty string – exactly what StringSplitOptions.RemoveEmptyEntriesshould prevent.
所以它失败了,因为"Three"我们有一个空字符串而不是,这正是StringSplitOptions.RemoveEmptyEntries应该防止的。
采纳答案by TJHeuvel
Most likely because you change the string after the split. You trim the values after splitting them, RemoveEmptyEntriesdoesn't consider the string " "empty.
很可能是因为您在拆分后更改了字符串。拆分后修剪值,RemoveEmptyEntries不认为字符串为" "空。
The following would achieve what you want, basically creating your own strip empty elements:
以下将实现您想要的,基本上创建您自己的条形空元素:
var tagsSplit = tags.Split(',').
Select(tag => tag.Trim()).
Where( tag => !string.IsNullOrEmpty(tag));
回答by Tilak
var tagsSplit = tags.Split(',')
.Where(str => str != String.IsNullOrWhiteSpace(str))
.Select(s => s.Trim());
回答by archil
Adjacent delimiters yield an array element that contains an empty string (""). The values of the StringSplitOptions enumeration specify whether an array element that contains an empty string is included in the returned array.
相邻的分隔符产生一个包含空字符串 ("")的数组元素。StringSplitOptions 枚举的值指定返回的数组中是否包含包含空字符串的数组元素。
" "by definition is not empty(it is actually whitespace), so it is not removed from resulting array.
" "根据定义不是空的(它实际上是whitespace),所以它不会从结果数组中删除。
If you use .net framework 4, you could work around that by using string.IsNullOrWhitespace method
如果你使用 .net framework 4,你可以通过使用string.IsNullOrWhitespace 方法来解决这个问题
var tagsSplit = tags.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries)
.Where(x => !string.IsNullOrWhiteSpace(x))
.Select(s => s.Trim());
回答by Coofucoo
RemoveEmptyEntries do not means space.
Your input string include many "space". You should notice that "space" is not empty. In computer, space is a special ASCII code. so the code:
RemoveEmptyEntries 并不意味着空间。
您的输入字符串包含许多“空格”。您应该注意到“空间”不是空的。在计算机中,空格是一种特殊的 ASCII 码。所以代码:
var tagsSplit = tags.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries)
.Select(s => s.Trim());
means:
方法:
- Split the input by ',' and remove empty entry, not include space. So you got an array with some space elements.
- Then you do trim for each of elements. The space elements become to empty.
- 按 ',' 拆分输入并删除空条目,不包括空格。所以你得到了一个包含一些空间元素的数组。
- 然后你对每个元素进行修剪。空间元素变为空。
That's why you got it.
这就是你得到它的原因。
回答by onlyvix.blogspot.com
Try
尝试
var tagsSplit = tags.Split(new[] { ',', ' ' }, StringSplitOptions.RemoveEmptyEntries);
This will spit by comma and space, and eliminate empty strings.
这将吐出逗号和空格,并消除空字符串。
回答by Teodor Tite
I've searched also for a clean way to exclude whitespace-entries duringa Split, but since all options seemed like some kind of workarounds, I've chosen to exclude them when looping over the array.
我还搜索了一种在拆分期间排除空白条目的干净方法,但是由于所有选项似乎都是某种解决方法,因此我选择在循环数组时排除它们。
string[] tagsSplit = tags.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
foreach (string tag in tagsSplit.Where(t => !string.IsNullOrWhiteSpace(t))) { }
I think this looks cleaner and - as a bonus - .Split(...).ToArray()may be ommited.
Of course, it is an option only when you may loop just after split and do not have to store entries for later use.
我认为这看起来更干净,而且 - 作为奖励 -.Split(...).ToArray()可以省略。当然,只有当您可以在拆分后立即循环并且不必存储条目以供以后使用时,它才是一个选项。
回答by JamesHoux
As this is a very common need, I went ahead and wrapped the most popular answer in a string extension method:
由于这是一个非常常见的需求,我继续将最受欢迎的答案包装在字符串扩展方法中:
public static IEnumerable<string> Split_RemoveWhiteTokens(this string s, params char[] separator)
{
return s.Split(separator).
Select(tag => tag.Trim()).
Where(tag => !string.IsNullOrEmpty(tag));
}
To split on ',' as the other examples, use like this:
要拆分 ',' 作为其他示例,请像这样使用:
var result = yourString.Split_RemoveWhiteTokens(',')
Note that the return type is IEnumerable, so you can do additional LINQ queries directly on the return result. Call .ToList() if you want to cast the result to a list.
请注意,返回类型是 IEnumerable,因此您可以直接对返回结果执行其他 LINQ 查询。如果要将结果转换为列表,请调用 .ToList()。

