I am new to strings and I am confused how string are splitting up for eg: in my code here I want to break a string with , and it's splitting into 9 parts but I am confused how it's splitting up please some one explain to me how it's breaking up:

string s31 = ",ONE,,TWO,,,THREE,,"; 

string[] s32 = s31.Split(ch31, StringSplitOptions.None);

o/p

""
one
""
two
""
""
three
""
""

1 Answers

3
Louis Matthijssen On

The split function is creating a new element on every occurrence of the comma. There are 8 commas in your string, so including the element before the first comma there will be 9 elements.

If you just want three elements, use StringSplitOptions.RemoveEmptyEntries, which will ignore the empty elements.