Quantcast
Channel: Coding and Programing » string
Viewing all articles
Browse latest Browse all 10

split string by multi-character delimiter in C#

$
0
0

Problem And Question

What if I want to split a string using a delimiter that is a word?

For example, “This is a sentence”.

I want to split on “is”. So I will get “This ” and ” a sentence”.

In Java, I can send in a string as a delimiter, but how do I accomplish this in C#?

Best Solution And Answer

http://msdn.microsoft.com/en-us/library/system.string.split.aspx

Example from the docs:

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

// ...
result = source.Split(stringSeparators, StringSplitOptions.None);

foreach (string s in result)
{
    Console.Write("'{0}' ", String.IsNullOrEmpty(s) ? "<>" : s);
}

Viewing all articles
Browse latest Browse all 10

Trending Articles