How i can split with 2 characters?

EvansGxz 121 Reputation points
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

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,231 questions
{count} votes

Accepted answer
  1. Olaf Helper 40,741 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