• Fabian Wetzel
  • 2011-09-01
  • 1 min. read

String.Split() Kwik-e

While I was cleaning up some legacy code I found an overly complex usage of String.Split() in several places:

1
2
var sentence = "Hello World!";
var words = sentence.Split(new char[] { ' ' });

If you take a look at the available overloads on Split() you may miss the params keyword. In fact it isn’t even visible on the overview page:

image

But if you take a closer look on Split(char[]) you will see this:

image

The params keyword tells you that the method accepts any number of arguments of type char. So you could also provide only one, which will simplify the given sample to:

1
2
var sentence = "Hello World!";
var words = sentence.Split(' ');