F
F
flash6612021-09-30 11:57:38
C++ / C#
flash661, 2021-09-30 11:57:38

What happens if after the delimiter comes the next delimiter in c# in the split method?

I have an array of delimiters. And a line with 2 delimiters in a row. Why does it give me 2 spaces?

char[] separators = new char[] {','};
string str = "2,,";
string[] outp = str.Split(separators);
Console.WriteLine(outp.Length);
 for (int i = 0; i < outp.Length; i++)
{
       Console.WriteLine(outp[i]);
 }

Can you please explain in more detail how split works. I know how to get rid of spaces, but why is the space displayed, and not the conditional "," . In the case when the separator is a space and then there is another 1 space, I still understand why a space is displayed, but in this situation not quite.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Voland69, 2021-09-30
@flash661

Your example in .NET 5 works correctly: it returns 2 and two empty strings (see the Length property), that is, there are no spaces.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question