Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is string.Split(';') valid but string.Split(':',StringSplitOptions.RemoveEmptyEntries) is not?

Tags:

c#

.net

.net-4.0

I'm confused which overload string.Split(';') is resolving to; I don't think ';' is a char array is it?

However, this is compiling OK but if I try to add a 2nd StringSplitOptions parameter no appropriate overload is found.

I don't like having to do new char []{';'} is it avoidable?

like image 792
Mr. Boy Avatar asked Oct 14 '15 11:10

Mr. Boy


People also ask

How to split a string with a delimiter in c#?

In C#, Split() is a string class method. The Split() method returns an array of strings generated by splitting of original string separated by the delimiters passed as a parameter in Split() method. The delimiters can be a character or an array of characters or an array of strings.

How to split string with separator?

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. If (" ") is used as separator, the string is split between words.

How does split method work?

The Split method extracts the substrings in this string that are delimited by one or more of the strings in the separator parameter, and returns those substrings as elements of an array. The Split method looks for delimiters by performing comparisons using case-sensitive ordinal sort rules.


2 Answers

The first overload of String.Split() has parameters defined as

params char[] separator

This means you can pass any number of char parameters and it will work:

someString.Split('1', '2', '3');
// or just one separator
someString.Split(';');

The second overload is different, it defines parameters like this

char[] separator, StringSplitOptions options

Notice, there is no params. Which require you to pass char[] parameter:

someString.Split(new[] {'1', '2', '3'}, StringSplitOptions.None);
// or just one separator
someString.Split(new[] {';'}, StringSplitOptions.None);
like image 125
Sinatr Avatar answered Oct 17 '22 17:10

Sinatr


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:

  1. string.Split()
  2. params keyword
  3. 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.

like image 1
Pawel Maga Avatar answered Oct 17 '22 17:10

Pawel Maga