-4

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
""
""
10
  • 4
    What's your question ? Do you wonder how split function works ?
    – Mennan
    May 1, 2019 at 9:24
  • What do you mean "how is it splitting up"? The question is not very clear. What's your issue with this code? May 1, 2019 at 9:24
  • how it's spliting into 9 parts ? @Mennan May 1, 2019 at 9:24
  • 4
    "how it's spliting into 9 parts" -- there are 8 commas in your input. If you take a piece of wood and saw it 8 times, you end up with 9 pieces of wood.
    – canton7
    May 1, 2019 at 9:25
  • 1
    How it does it - here's the reference source: github.com/Microsoft/referencesource/blob/master/mscorlib/…
    – Sean Reid
    May 1, 2019 at 9:26

1 Answer 1

3

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.