8

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.

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.

TylerH
  • 20,799
  • 66
  • 75
  • 101
GameScripting
  • 16,092
  • 13
  • 59
  • 98
  • 3
    I would imagine there are any number of seemingly simple methods that could be added, but that is why you can add your own via extension methods, as you point out. – glosrob Jan 13 '13 at 10:31
  • As you can tell from the responses, it is unlikely you'll get an answer to this question. My *guess* is that it has something to do with a usability study where the test subjects got into trouble somehow, possibly due to not dealing with the Char vs String overload properly. Only the .NET framework designers could accurately answer it, they don't post here. – Hans Passant Jan 13 '13 at 11:05
  • Seriously? Why did you not include this specific overload that I think is useful? There must be 1000s of these. – glosrob Jan 13 '13 at 11:13
  • @glosrob I don't think so. Often there are valid reasons such a thread safety or usability studies as pointed out by Hans Passant. – GameScripting Jan 13 '13 at 11:18
  • I'd be interested to see if you get an answer beyond 'we cannot provide every possible overload of every method in the framework' :) – glosrob Jan 13 '13 at 11:27
  • 2
    @glosrob While it's certainly true that there must be 1000s of overloads that people would like to see and that weren't provided, I think `string.Split(string)` in particular seems to be a complete no-brainer and I wonder too if there was a specific reason for not including it. And handling this with an extension method would still involve the extra overhead of putting the string in an array. – JLRishe Jan 13 '13 at 11:29
  • 1
    I don't know why the BCL team made that decision. I personally find it irritating that every overload of Split requires me to create garbage in the arguments. My suggestion is that you ask the BCL team via their blog. This question requires mind-reading to answer, and is therefore likely to be closed. – Eric Lippert Jan 13 '13 at 16:10

2 Answers2

3

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.

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.

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.

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.

thudbutt
  • 1,481
  • 1
  • 19
  • 32
  • 2
    The [`StringSplitOptions`](http://msdn.microsoft.com/en-us/library/system.stringsplitoptions.aspx) really isn't that advanced, its only members are `None` and `RemoveEmptyEntries`. – CodeCaster Jan 17 '13 at 21:57
-2
string input = "This - is - an - example";
string[] splitted = Regex.Split(input," - ");
foreach (string word in splitted)
{
    MessageBox.Show(word);
}

it's not only the matter of spaces, it exatcly matches your string seperator, look

string input = "This,- is,- a,- complicated,- example";
string[] splitted = Regex.Split(input,",- ");
foreach (string word in splitted)
{
    MessageBox.Show(word);
}
Mahdi Tahsildari
  • 13,065
  • 14
  • 55
  • 94
  • 2
    `Regex` is mutch slower that `String.Split` and [one should not use `Regex` if it's not about regular expressions](http://stackoverflow.com/a/2234376/808723). – GameScripting Jan 13 '13 at 11:07
  • 1
    @GameScripting the link you provided doesn't explain why one shouldn't use Regex if it's not about regular expressions. – default Jan 13 '13 at 11:30
  • @Default Have a look at the comments. They are stating out exactly the same. I think use `Regex` for splitting strings may [confuse further developers](http://stackoverflow.com/a/1257322/808723) since there's noting about regular expressions. – GameScripting Jan 13 '13 at 11:36
  • 1
    the only comment stating that is *"Since the string isn't really a regex, there's no point in invoking the regex parser."* - that's not a reason to why it should not be used (or rather, it's a pretty bad one IMHO). The link you provided about confusing further developers does confuse me. Next time, may I suggest that you just explain what you want to say instead of using SO answers where I need to guess what you are trying to say. – default Jan 13 '13 at 11:43
  • I do this to prove I'm not the only one with that cerain opinion. If I want to explain why one should not use something because its not made to be used that way, I'd have to write a blog post about it, it's not about a single line. And since all these things have been stated out already I'm refering to them. However I'll try to follow your advice and try to be more clear about what I want to say. – GameScripting Jan 13 '13 at 12:06