在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[] { ' ', '\t' };
string[] ssizes = myStr.Split(whitespace);
这是令人厌烦的在我的code处处定义的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);
或
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.
的如果分隔参数是空
或不包含字符,空白字符被认为是分隔符。空格字符由的Uni code标准定义,并返回真正
如果它们被传递到<一个href=\"http://msdn.microsoft.com/en-us/library/system.char.iswhitespace.aspx\"><$c$c>Char.IsWhiteSpace$c$c>方法的
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屋!