In the first case you are using this method:
public string[] Split(params char[] separator)
In the second case:
public string[] Split(char[] separator, StringSplitOptions options)
As you can see the declaration in the second case is a little different, so you can't pass parameters in this way.
If you want to use this method in a similar way, you can write your own extension method:
public static class StringExtensions
{
public static string[] Split(this string s, char separator, StringSplitOptions options)
{
return s.Split(new[] { separator }, options);
}
//or
public static string[] Split(this string s, StringSplitOptions options, params char[] separator)
{
return s.Split(separator, options);
}
}
And use them like below:
var s = "asdasd;asd;;";
var split = s.Split(StringSplitOptions.RemoveEmptyEntries, ';');
var split2 = s.Split(StringSplitOptions.RemoveEmptyEntries, ';', ',');
var split3 = s.Split(';', StringSplitOptions.RemoveEmptyEntries);
I suggest you to read more about this elements:
- string.Split()
- params keyword
- params keyword explanations: link
@Groo params keyword explanation:
The docs for params don't actually explain how/when the array gets created, so that's what's probably confusing. The thing is that params is just a hint to the compiler that you are allowed to pass your array's items separated by comma, without explicitly instantiating the array. But a new array containing your parameters is created on each call to the Split method, regardless of whether you are passing 0, 1 or many parameters. Also, you can still create an array yourself and pass it to the method, but this lets the compiler do it for you.