[英]Why does C# split give me an array ending in an empty line?
I have the following expression: 我有以下表达式:
"<p>What ?</p>\n<pre>Starting Mini</pre>"
When I perform a split as follows: 当我按如下方式执行拆分时:
var split = content
.Split(new[] { "<pre>", "</pre>" }, StringSplitOptions.None);
Then it gives me three entries: 然后它给了我三个条目:
"<p>What ?</p>\n"
"Starting Mini"
""
Why does it give an empty line as the third entry and how can I avoid this? 为什么它将空行作为第三个条目,我该如何避免这种情况?
The "why" is simply: the input (if you don't remove empty entries) will always "split" at any occurrence of the separator(s), so if the separator(s) appear n
times in the string, then the array will be n+1
long. “为什么”只是:输入(如果你不删除空条目)将始终在任何分隔符出现时“拆分”,所以如果分隔符在字符串中出现n
次,那么数组将是n+1
长。 In particular, this essentially lets you know where they occurred in the original string (although when using multiple separators, it doesn't let you know which appeared where). 特别是,这基本上可以让你知道他们发生于原始的字符串(虽然使用多个分离器时,它不会让你知道哪些地方出现)。
For example, with a simple example (csv without any escaping etc): 例如,使用一个简单的例子(没有任何转义的csv等):
string[] arr = input.Split(','); // even if something like "a,b,c,d,"
// which allows...
int numberOfCommas = arr.Length - 1;
string original = string.Join(",", arr);
The fix is, as already mentioned, to use RemoveEmptyEntries
. 如前所述,修复程序是使用RemoveEmptyEntries
。
Use StringSplitOptions.RemoveEmptyEntries
instead to remove empty string in list 使用StringSplitOptions.RemoveEmptyEntries
来删除列表中的空字符串
var split = content
.Split(new[] { "<pre>", "</pre>" }, StringSplitOptions.RemoveEmptyEntries);
You get this behaviour as specified from Microsoft: "Adjacent delimiters yield an array element that contains an empty string ("")." 您得到Microsoft指定的此行为: “相邻分隔符产生一个包含空字符串(”“)的数组元素。” So since you have the last pre you get the last empty array element 所以既然你有最后一个预备版,你就得到了最后一个空数组元素
The reason you are getting this behaviour is that your one of the delimeter </pre>
happens to exist at the end of the string . 您获得此行为的原因是您的其中一个分隔符</pre>
恰好存在于字符串的末尾 。
You may see: string.Split - MSDN 你可能会看到: string.Split - MSDN
... a delimiter is found at the beginning or end of this instance, the corresponding array element contains Empty ... 在此实例的开头或结尾处找到分隔符 ,相应的数组元素包含Empty
To overcome this: 要克服这个:
Use StringSplitOptions.RemoveEmptyEntries
instead of StringSplitOptions.None
使用StringSplitOptions.RemoveEmptyEntries
而不是StringSplitOptions.None
StringSplitOptions.RemoveEmptyEntries - MSDN StringSplitOptions.RemoveEmptyEntries - MSDN
The return value does not include array elements that contain an empty string 返回值不包含包含空字符串的数组元素
var split = content
.Split(new[] { "<pre>", "</pre>" }, StringSplitOptions.RemoveEmptyEntries);
Mailou, instead of giving 'StringSplitOptions.None' try 'StringSplitOptions.RemoveEmptyEntries'. Mailou,而不是给'StringSplitOptions.None'尝试'StringSplitOptions.RemoveEmptyEntries'。 It removes the the empty lines. 它删除了空行。
您还需要指定
StringSplitOptions.RemoveEmptyEntries enumerator.
The split string[] values not include any empty string by using StringSplitOptions.RemoveEmptyEntries 通过使用StringSplitOptions.RemoveEmptyEntries ,split string []值不包含任何空字符串
var split = content
.Split(new[] { "<pre>", "</pre>" }, StringSplitOptions.RemoveEmptyEntries);
Reference: StringSplitOptions Enumeration 参考: StringSplitOptions枚举
You are getting empty line due to 你得到空行了
</pre>
You are instructing split
function to split by <pre>
and </pre>
您正在通过<pre>
和</pre>
指示split
功能
As result with <pre>
you are getting 结果<pre>
你得到了
<p>What ?</p>\n
Starting Mini</pre>
And next result is with </pre>
is 接下来的结果是</pre>
是
<p>What ?</p>\n
Starting Mini
...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.