How i can split with 2 characters?

Anonymous
2021-06-29T13:08:13.457+00:00

I have this example code, but this only work with 1 char, i want to split with ", " that mean have 2 characters.

string s = "You, win, some.";

string[] subs = s.Split(',');

foreach (var sub in subs)
{
    Console.WriteLine($"Substring: {sub}");
}

Result:
Substring: You
Substring: win
Substring: some.

If you look, the second and third result have a space in the beginning
but if i do:

 string[] subs = s.Split(', ');

I have this error:
(3,25): error CS1012: Too many characters in character literal

Developer technologies C#
{count} votes

Accepted answer
  1. Olaf Helper 47,436 Reputation points
    2021-06-29T13:15:55.713+00:00

    Too many characters in character literal

    You used apostrophe = character and that can be only one char.

    You can also pass a string array to Split function, and in a string you can use more then one char:

    var sep = new string[] { ", " };
    string[] subs = s.Split(sep, StringSplitOptions.None);
    
    1 person found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.