- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我正在检查给定文本文件中的行。行可能有随机空格,我只对检查行中的单词数而不是空格感兴趣。我这样做:
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”)
为了完整起见,new char[0]
提供参数是为了访问允许指定 StringSplitOptions
的重载.为了使用默认分隔符,分隔符参数必须是 null
或零长度。但是,在这种情况下使用 null
将满足多重重载,因此您必须指定有效类型(char[]
或 string[]
)。这可以通过多种方式完成,例如 (char[])null
或 null as char[]
,或者像上面那样使用零长度字符数组。
关于c# - 使用不带参数的 Split 和 RemoveEmptyEntries 选项之间的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21047886/
我过去曾多次遇到过这种情况,最终决定找出原因。 StringSplitOptions.RemoveEmptyEntries 会建议它删除空条目。 那么为什么这个测试会失败呢? var tags = "
我最近学习了 TextFieldParser 来解析 words,而以前我会使用 string.Split 来这样做。我有一个关于新学的 class 的问题。 如果我们使用 string.Split
我正在检查给定文本文件中的行。行可能有随机空格,我只对检查行中的单词数而不是空格感兴趣。我这样做: string[] arrParts = strLine.Trim().Split(); if (ar
我有代码(在本例中针对 .NET Core 3.1),我想在空白处拆分字符串。我的草稿代码是 string s = "A B"; var parts = s.Split(null, StringSpl
我有代码(在本例中针对 .NET Core 3.1),我想在空白处拆分字符串。我的草稿代码是 string s = "A B"; var parts = s.Split(null, StringSpl
作为一名 Java 新手,我正在为 String.split 苦苦挣扎。尝试标记以下字符串 "(3,3,{S,W,P},{P,W,P},{P,P,P}),(1,2,{S,E}),(2,1,{{S},{
我很困惑 string.Split(';') 正在解析哪个重载;我不认为 ';' 是一个字符数组,对吗? 然而,这编译正常,但如果我尝试添加第二个 StringSplitOptions 参数,则找不到
我是一名优秀的程序员,十分优秀!