Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

split string with more than one Char in C#

Tags:

string

c#

split

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?

like image 660
Rodniko Avatar asked Oct 07 '10 09:10

Rodniko


People also ask

How do I split a string into more than one character?

Use the String. split() method to split a string with multiple separators, e.g. str. split(/[-_]+/) . The split method can be passed a regular expression containing multiple characters to split the string with multiple separators.

Can a char be more than one character in C?

C allows users to store words and sentences with the help of the char data type. Character data type stores only a single character, but a person's name has more than one character. We can create an array of characters to store such data with more than one character in C.

Can you split a string in C?

In C, the strtok() function is used to split a string into a series of tokens based on a particular delimiter. A token is a substring extracted from the original string.

Can char have multiple characters?

Also, there is a multi-character literal that contains more than one c-char. A single c-char literal has type char and a multi-character literal is conditionally-supported, has type int, and has an implementation-defined value.


2 Answers

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.

like image 123
Nayan Avatar answered Oct 06 '22 15:10

Nayan


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
like image 5
Mark Byers Avatar answered Oct 06 '22 14:10

Mark Byers