在String.Split操作指定空格最好的方法 [英] Best way to specify whitespace in a String.Split operation

查看:160
本文介绍了在String.Split操作指定空格最好的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是分割的基础上空白的字符串如下:

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方法

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.

总是,总是,总是阅读文档!

Always, always, always read the documentation!

这篇关于在String.Split操作指定空格最好的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆