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.
The split() method splits a string into an array of substrings. The split() method returns the new array. The split() method does not change the original string.
The split() method breaks up a string at the specified separator and returns a list of strings.
If the delimiter is an empty string, the split() method will return an array of elements, one element for each character of string. If you specify an empty string for string, the split() method will return an empty string and not an array of strings.
Use strtok() function to split strings strtok(): A strtok() function is used to split the original string into pieces or tokens based on the delimiter passed.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With