在 String.Split 操作中指定空格的最佳方法 [英] Best way to specify whitespace in a String.Split operation
问题描述
我正在根据空格拆分字符串,如下所示:
I am splitting a string based on whitespace as follows:
string myStr = "The quick brown fox jumps over the lazy dog";
char[] whitespace = new char[] { ' ', ' ' };
string[] ssizes = myStr.Split(whitespace);
在我想这样做的代码中到处定义 char[] 数组很烦人.有没有更高效的方法不需要创建字符数组(如果复制到不同的地方容易出错)?
It's irksome to define the char[] array everywhere in my code I want to do this. Is there more efficent way that doesn't require the creation of the character array (which is prone to error if copied in different places)?
推荐答案
如果你只是打电话:
string[] ssize = myStr.Split(null); //Or myStr.Split()
或:
string[] ssize = myStr.Split(new char[0]);
然后假设空格是分割字符.来自 string.Split(char[])代码>方法的文档页面
.
then white-space is assumed to be the splitting character. From the string.Split(char[])
method's documentation page.
如果分隔符参数为 null
或不包含任何字符,则假定空白字符为分隔符.空白字符由 Unicode 标准定义,如果它们被传递到 Char.IsWhiteSpace
方法.
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 returntrue
if they are passed to theChar.IsWhiteSpace
method.
总是,总是,总是阅读文档!
Always, always, always read the documentation!
这篇关于在 String.Split 操作中指定空格的最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!