在 String.Split 操作中指定空格的最佳方法
我根据空格分割字符串,如下所示:
string myStr = "The quick brown fox jumps over the lazy dog";
char[] whitespace = new char[] { ' ', '\t' };
string[] ssizes = myStr.Split(whitespace);
在我想要执行此操作的代码中到处定义 char[] 数组是很烦人的。是否有更有效的方法不需要创建字符数组(如果复制到不同的地方很容易出错)?
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);
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)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(12)
您可以这样做:
MSDN 有更多示例和参考:
http://msdn.microsoft .com/en-us/library/b873y76a.aspx
You can just do:
MSDN has more examples and references:
http://msdn.microsoft.com/en-us/library/b873y76a.aspx
如果您只调用:
或:,
则假定空格为分割字符。来自
string.Split(char[]) 方法的文档页面
。
始终、始终、始终阅读文档!
If you just call:
or:
then white-space is assumed to be the splitting character. From the
string.Split(char[])
method's documentation page.Always, always, always read the documentation!
是的,这里还需要一个答案!
到目前为止,所有解决方案都解决了相当有限的规范输入领域,即:单个空白字符之间元素(尽管向@cherno致敬,至少提到了这个问题)。
但我认为,除了最模糊的场景之外,拆分所有这些应该会产生相同的结果:
String.Split
(在此处的其他答案中显示的任何风格中) ) 根本无法正常工作,除非您将RemoveEmptyEntries
选项附加到以下任一选项:如图所示,省略该选项会产生四种不同的结果(标记为 A、B、C 和 D)。所有四个的单一结果使用
RemoveEmptyEntries
时的输入:当然,如果您不这样做不喜欢使用选项,只需使用正则表达式替代:-)
Yes, There is need for one more answer here!
All the solutions thus far address the rather limited domain of canonical input, to wit: a single whitespace character between elements (though tip of the hat to @cherno for at least mentioning the problem).
But I submit that in all but the most obscure scenarios, splitting all of these should yield identical results:
String.Split
(in any of the flavors shown throughout the other answers here) simply does not work well unless you attach theRemoveEmptyEntries
option with either of these:As the illustration reveals, omitting the option yields four different results (labeled A, B, C, and D) vs. the single result from all four inputs when you use
RemoveEmptyEntries
:Of course, if you don't like using options, just use the regex alternative :-)
根据文档:
因此,只需调用 myStr.Split(); 无需传递任何内容,因为 separator 是一个
params
数组。According to the documentation :
So just call
myStr.Split();
There's no need to pass in anything because separator is aparams
array.为什么不使用?:
Why dont you use?:
请注意,即使使用
String.Split(null)
,相邻的空格也不会被视为单个分隔符。如果您的任何标记由多个空格或制表符分隔,您将在数组中返回空字符串。从文档中:
Note that adjacent whitespace will NOT be treated as a single delimiter, even when using
String.Split(null)
. If any of your tokens are separated with multiple spaces or tabs, you'll get empty strings returned in your array.From the documentation:
所以不要复制和粘贴!提取一个函数来进行拆分并重用它。
代码重用是你的朋友。
So don't copy and paste! Extract a function to do your splitting and reuse it.
Code reuse is your friend.
你可以使用
分割字符串并在空格之前获取其第一次出现的位置。
You can use
to split a string and get its first occurrence before the space.
你不能内嵌吗?
否则,如果您经常执行此操作,则始终可以创建常量或包含该 char 数组的内容。
正如其他人指出的那样,您也可以根据文档使用
null
或空数组。当您这样做时,它将自动使用空白字符。Can't you do it inline?
Otherwise, if you do this exact thing often, you could always create constant or something containing that char array.
As others have noted you can according to the documentation also use
null
or an empty array. When you do that it will use whitespace characters automatically.你为什么不这样做:
似乎有一个方法
String .NET 4.0 中的 .ToCharArray()
!编辑:正如 VMAtm 所指出的,该方法已经存在于.NET 2.0中!
Why don't you just do this:
It seems there is a method
String.ToCharArray()
in .NET 4.0!EDIT: As VMAtm has pointed out, the method already existed in .NET 2.0!
如果出现重复相同代码的问题,请在封装拆分逻辑的 String 类上编写扩展方法。
If repeating the same code is the issue, write an extension method on the String class that encapsulates the splitting logic.
在 C# 中,您可以使用下面的代码。
In C# you can use Below Code.