How can I split a string with a string delimiter?

markzzz picture markzzz · Jan 19, 2012 · Viewed 684.9k times · Source

I have this string:

My name is Marco and I'm from Italy

I'd like to split it, with delimiter is Marco and, so I should get an array with

  • My name at [0] and
  • I'm from Italy at [1].

How can I do it with C#?

I tried with:

.Split("is Marco and")

But it wants only a single char.

Answer

juergen d picture juergen d · Jan 19, 2012
string[] tokens = str.Split(new[] { "is Marco and" }, StringSplitOptions.None);

If you have a single character delimiter (like for instance ,), you can reduce that to (note the single quotes):

string[] tokens = str.Split(',');