为什么没有一个string.Split(串)的过载? [英] Why isn't there a string.Split(string) overload?

查看:155
本文介绍了为什么没有一个string.Split(串)的过载?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么站得住脚的理由有不的 String.Split 它接受一个分隔符字符串和文本进行拆分?

Are there any valid reasons why there isn't an overload of the String.Split which accepts a delimiter string and a text to be split?

string[] Split(string delimiter)

然后可以使用像

which could then be used like

string input = "This - is - an - example";
string[] splitted = input.Split(" - ");
// results in:
//  { "This", "is", "an", "example" }

我真的知道,我可以很容易地创建一个延伸的方法,但必须有,为什么这不能被添加站得住脚的理由。

I really know, that I can create an extention method easily, but there must be valid reason why this has not been added.

请注意,我不是在寻找如何使用字符串分隔符来分割字符串的解决方案,我宁愿找一个解释,为什么这样的过载可能会导致问题。这是因为我不认为这真的会导致问题,我发现它真的很难为初学者理解为什么我们必须通过实际的String [] 代替简单的字符串作为分隔符。

Please note, that I am not looking for a solution of how to split a string using a string delimiter, I am rather looking for an explanation, why such an overload could cause problems. This is because I don't think it would really cause problems and I find it really hard for a beginners to understand why we have to pass an actual string[] instead of a simple string as a delimiter.

推荐答案

调整的问题是为什么StringSplitOptions参数强制使用String []参数调用String.Split()的时候?也许能解答你的问题。

Tweaking the question to be "Why is the StringSplitOptions parameter compulsory when calling String.Split() with a String[] argument?" might provide an answer to your question.

请注意,实际上不是一个String.Split()重载,它接受一个字符。该重载采用一个char [],但因为它是一个参数数组,你可以用一个字称呼它,它是隐式转换为一个char []。例如,

Note that there's not actually a String.Split() overload which accepts a single character. The overload takes a Char[] but as it's a params array you can call it with a single character and it is implicitly cast to a Char[]. e.g.

"1,2,3,4,5".Split(',');

调用相同的斯普利特()的过载为

calls the same Split() overload as

"1,2,3,4,5".Split(new[] { ',' });

如果有斯普利特(),它接受一个字符串参数[]那么你就能够调用传递一个字符串参数分裂。过载

If there were an overload of Split() which accepted a single argument of String[] then you would be able to call Split by passing a single string argument.

但是,这种超载不存在,并通过一个String []斯普利特时StringSplitOptions是强制性的。至于为什么StringSplitOptions是强制性的,我只能推论,但它可以是与一个字符串分割时,一个复杂的分裂的算法的可能性来处理增加显著。为了提供这些情况下预期的结果,它是preferable的方法,发现多定界符彼此相邻时的行为,被定义。即StringSplitOptions是强制性的。

However that overload doesn't exist and StringSplitOptions is compulsory when passing a String[] to Split. As to why StringSplitOptions is compulsory, I can only theorize but it may be that when splitting with a string, the likelihood of a complex split for the algorithm to deal with increases significantly. To provide expected results for these cases, it is preferable for the behaviour of the method, when finding multiple delimiters next to each other, to be defined. i.e. StringSplitOptions is compulsory.

您可能会说,你可以有一个斯普利特(字符串,StringSplitOptions)过载,但伊利亚·伊万诺夫在回答上面提到的,你需要停下来的地方,并有通过一个单一的字符串中的一个完美的方法。

You might argue that you could have a Split(String, StringSplitOptions) overload, but as Ilya Ivanov mentioned in the answer above, you need to stop somewhere and there is a perfectly good way of passing a single string in.

这篇关于为什么没有一个string.Split(串)的过载?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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