89

I have a string "4,6,8\n9,4"

I want to split this based on ,and \n

Output array should be

4
6
8
9
4

Edit :

Now i am reading string from console , when i enter a string as above in console , in the code behind i get as "4,6,8\\n9,4" . Now that i want to split using "," and "\\n" . How can i change the expression ?

Kuntady Nithesh
  • 11,371
  • 20
  • 63
  • 86

7 Answers7

162

Use string.Split(char [])

string strings = "4,6,8\n9,4";
string [] split = strings .Split(new Char [] {',' , '\n' });

EDIT

Try following if you get any unnecessary empty items. String.Split Method (String[], StringSplitOptions)

string [] split = strings .Split(new Char [] {',' , '\n' }, 
                                 StringSplitOptions.RemoveEmptyEntries);

EDIT2

This works for your updated question. Add all the necessary split characters to the char [].

string [] split = strings.Split(new Char[] { ',', '\\', '\n' },
                                 StringSplitOptions.RemoveEmptyEntries);
CharithJ
  • 46,289
  • 20
  • 116
  • 131
  • I could not get this to work using `.Split(new Char[]{'?='})`, but doing this helped http://stackoverflow.com/questions/1126915/how-do-i-split-a-string-by-a-multi-character-delimiter-in-c. Great answer none the less – CSharper Oct 09 '14 at 16:04
  • 1
    @CSharper You're doing it wrong. You should do `.Split(new Char[]{'?','='})` – zdimension Feb 25 '16 at 13:57
  • 1
    Is there a way to know which character in the array was used to split the string? (assuming you need to know this value?) – Robert Green MBA Feb 11 '18 at 21:11
  • @RobertGreenMBA No standard method. You can use split method for each character. Create your own method for your requirement. – CharithJ Feb 11 '18 at 21:29
  • 1
    ReSharper will recommend this format: `new[] { ',', '\\', '\n' }` – Marcel Gruber Mar 24 '21 at 21:07
12

Another option is to use Regex.Split. This is useful when the split sequences are more complex. For instance if spaces can also be part of the split delimiters such as:

"4,6,8 , 9\\n\\n4"

Then:

using System.Text.RegularExpressions;
var i = "4,6,8 , 9\n\n4";
var o = Regex.Split(i, @"[,\s\n]+");
// now o is:
// new string[] { "4", "6", "8", "9" }

Note that the regular expression used is "more accepting" - it ignored the empty "space" between the \n's and it would accept "4 6 8 9 4" just the same - so the above to to show a point: there is more than one way to skin a cat.

Happy coding.

7
var s = "4,6,8\n9,4";
var split = s.Split(new char[]{',', '\n'});

But this has to be a dupe...

EDIT: Addressing the comment.

This code:

static void Main(string[] args)
{
    var s = "4,6,8\n9,4";

    foreach (var a in s.Split(new char[] { ',', '\n' }))
        System.Diagnostics.Debug.WriteLine(a);
}

Outputs this:

4
6
8
9
4

EDIT: Reading input from the console is different. \n is different when entered manually.

static void Main(string[] args)
{
    var s = "4,6,8\\n9,4";

    foreach (var a in s.Split(new string[] { ",", "\\n" }, StringSplitOptions.RemoveEmptyEntries))
        System.Diagnostics.Debug.WriteLine(a);
}
Austin Salonen
  • 49,173
  • 15
  • 109
  • 139
4
string tosplit = "4,6,8\n9,4";
var split = tosplit.Split(new Char [] {',', '\n' });

Just in case you are not printing / seeing it properly:

split.ToList().ForEach(Console.WriteLine);
manojlds
  • 290,304
  • 63
  • 469
  • 417
2

Can you do a string.Replace('\n',',') followed by the string.split(',') ?

andrew
  • 1,184
  • 3
  • 19
  • 28
0
// input string
string input = "email@domain.com;email2@domain.com,email3@domain.com;";

// each character in this string will split it
string splitBy = ",;";

// do the split, remove any blank results
string[] result = input.Split(splitBy.ToCharArray(), StringSplitOptions.RemoveEmptyEntries);

output is an array of strings:

email@domain.com
email2@domain.com
email3@domain.com
Daniel Barnes
  • 163
  • 2
  • 9
0

for every cases

    string s = "";
    s.Split(new string[] { ",", "DEL", ";"}, StringSplitOptions.None);
elle0087
  • 840
  • 9
  • 23