How can I split a string with a string delimiter?

C#StringSplit

C# Problem Overview


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.

C# Solutions


Solution 1 - C#

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(',');

Solution 2 - C#

.Split(new string[] { "is Marco and" }, StringSplitOptions.None)

Consider the spaces surronding "is Marco and". Do you want to include the spaces in your result, or do you want them removed? It's quite possible that you want to use " is Marco and " as separator...

Solution 3 - C#

You are splitting a string on a fairly complex sub string. I'd use regular expressions instead of String.Split. The later is more for tokenizing you text.

For example:

var rx = new System.Text.RegularExpressions.Regex("is Marco and");
var array = rx.Split("My name is Marco and I'm from Italy");

Solution 4 - C#

Try this function instead.

string source = "My name is Marco and I'm from Italy";
string[] stringSeparators = new string[] {"is Marco and"};
var result = source.Split(stringSeparators, StringSplitOptions.None);

Solution 5 - C#

You could use the IndexOf method to get a location of the string, and split it using that position, and the length of the search string.


You can also use regular expression. A simple google search turned out with this

using System;
using System.Text.RegularExpressions;

class Program {
  static void Main() {
	string value = "cat\r\ndog\r\nanimal\r\nperson";
	// Split the string on line breaks.
	// ... The return value from Split is a string[] array.
	string[] lines = Regex.Split(value, "\r\n");

	foreach (string line in lines) {
	    Console.WriteLine(line);
    }
  }
}

Solution 6 - C#

Read C# Split String Examples - Dot Net Pearls and the solution can be something like:

var results = yourString.Split(new string[] { "is Marco and" }, StringSplitOptions.None);

Solution 7 - C#

There is a version of string.Split that takes an array of strings and a StringSplitOptions parameter:

http://msdn.microsoft.com/en-us/library/tabh47cf.aspx

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionmarkzzzView Question on Stackoverflow
Solution 1 - C#juergen dView Answer on Stackoverflow
Solution 2 - C#Anders Marzi TornbladView Answer on Stackoverflow
Solution 3 - C#HuusomView Answer on Stackoverflow
Solution 4 - C#DanTheManView Answer on Stackoverflow
Solution 5 - C#PatrickView Answer on Stackoverflow
Solution 6 - C#Guillaume SlashyView Answer on Stackoverflow
Solution 7 - C#Charles LambertView Answer on Stackoverflow