Pipeline
Pipeline

Reputation: 1059

Splitting on multiple characters at once

I know it is possible to split a string on a character like:

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

If I know the format of my strings are going to be comma delimited, or new line delimited, can I handle both of these are once? Can both be done at once?

I.e:

a, b, c, d, e, f, g

or:

a
b
c
d
e

or:

a, b, c, 
d, e 
f

How can I split on multiple characters at once?

Upvotes: 0

Views: 86

Answers (2)

EZI
EZI

Reputation: 15354

str.Split(",\n\r".ToCharArray());

insert any char you want to the string.

Upvotes: 1

Michael G
Michael G

Reputation: 6745

The string Split() method takes has an override that accepts an array of characters to split on.

string[] s = someString.Split(new [] { ',', '\n' });

Also, an optional parameter is the StringSplitOptions value. Which allows you to specify if you'd like to remove blank/empty entries.

Upvotes: 3

Related Questions