Rodniko
Rodniko

Reputation: 5124

split string with more than one Char in C#

i want to split the String = "Asaf_ER_Army" by the "ER" seperator. the Split function of String doesn't allow to split the string by more than one char.

how can i split a string by a 'more than one char' seperator?

Upvotes: 13

Views: 11767

Answers (3)

Mark Byers
Mark Byers

Reputation: 837926

String.Split does do what you want. Use the overload that takes a string array.

Example:

string[] result = "Asaf_ER_Army".Split(
    new string[] {"ER"},
    StringSplitOptions.None);

Result:

Asaf_
_Army

Upvotes: 6

Nayan
Nayan

Reputation: 3214

It does. Read here.

string source = "[stop]ONE[stop][stop]TWO[stop][stop][stop]THREE[stop][stop]";
string[] stringSeparators = new string[] {"[stop]"};

// Split a string delimited by another string and return all elements.
string[] result = source.Split(stringSeparators, StringSplitOptions.None);

Edit: Alternately, you can have some more complicated choices (RegEx). Here, http://dotnetperls.com/string-split.

Upvotes: 21

GôTô
GôTô

Reputation: 8053

There is an overload of String.Split which takes a string array as separators: http://msdn.microsoft.com/en-gb/library/1bwe3zdy%28v=VS.80%29.aspx

Unless you are using framework < 2?

Upvotes: 1

Related Questions