gpt4 book ai didi

c# - 使用不带参数的 Split 和 RemoveEmptyEntries 选项之间的区别

转载 作者:可可西里 更新时间:2023-11-01 09:11:51 36 4
gpt4 key购买 nike

我正在检查给定文本文件中的行。行可能有随机空格,我只对检查行中的单词数而不是空格感兴趣。我这样做:

string[] arrParts = strLine.Trim().Split();

if (arrParts.Length > 0)
{
...
}

现在,根据msdn,

If the separator parameter is null or contains no characters, white-space characters are assumed to be the delimiters. White-space characters are defined by the Unicode standard and return true if they are passed to the Char.IsWhiteSpace method.

IsWhiteSpace 方法涵盖许多不同形式的空白,包括常用的:' '\t 和\n

但是最近我看到使用了这种格式:

拆分(新字符[0],StringSplitOptions.RemoveEmptyEntries)

这有什么不同?

最佳答案

考虑以下字符串:

"Some  Words"//notice the double space

使用 Split()由于双空格,将按空格拆分并包含 3 个项目(“Some”、“”、“Words”)。

StringSplitOptions.RemoveEmptyEntries选项指示函数对空条目打折,因此它会产生 2 个项目(“Some”、“Words”)

Here is a working example


为了完整起见,new char[0]提供参数是为了访问允许指定 StringSplitOptions 的重载.为了使用默认分隔符,分隔符参数必须是 null或零长度。但是,在这种情况下使用 null将满足多重重载,因此您必须指定有效类型(char[]string[] )。这可以通过多种方式完成,例如 (char[])nullnull as char[] ,或者像上面那样使用零长度字符数组。

See here for more information

关于c# - 使用不带参数的 Split 和 RemoveEmptyEntries 选项之间的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21047886/

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