-4

I have this code:

char[] c = {','};
string[] s = someString.Split(c, StringSplitOptions.RemoveEmptyEntries);

I want to rewrite it as:

string[] s = someString.Split({ ',' }, StringSplitOptions.RemoveEmptyEntries);

but that gives a syntax error. Why can I not use { ',' } in a method call?

Bart Friederichs
  • 33,050
  • 15
  • 95
  • 195

2 Answers2

5
string[] s = someString.Split(new [] { ',' }, StringSplitOptions.RemoveEmptyEntries);
Dovydas Šopa
  • 2,282
  • 8
  • 26
  • 34
0

You need to create an instance of char array with the new keyword: new [] {','} or use another Split version and filter empty string afterwards:

var s = someString.Split(',').Where(i => !String.IsNullOrEmpty(i)).ToArray();
Ivan Yuriev
  • 488
  • 8
  • 14