308

I have this string:

"My name is Marco and I'm from Italy"

I'd like to split it, with the delimiter being 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.

jordanz
  • 367
  • 4
  • 12
markzzz
  • 47,390
  • 120
  • 299
  • 507
  • related https://stackoverflow.com/questions/315358/c-sharp-syntax-split-string-into-array-by-comma-convert-to-generic-list-and – barlop Jun 04 '17 at 18:59

7 Answers7

625
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(',');
juergen d
  • 201,996
  • 37
  • 293
  • 362
  • 1
    you can remove `string`: `.Split(new[] { "is Marco and" }, StringSplitOptions.None)` – pomber Jul 30 '15 at 19:42
  • 5
    `new string[]` is redundant in that case, you could just use `new []` – pomber Aug 19 '15 at 16:35
  • 9
    Note the single quotes in str.Split(','); instead of str.Split(","); It took me a while to notice – gsubiran Jul 12 '16 at 19:09
  • Me too regarding the single quotes... why is that necessary? – gunslingor Dec 18 '16 at 17:21
  • 2
    @user3656612 Because it accepts character (char), not a string. chars are surrounded by single quotes. – garethb Dec 22 '16 at 23:15
  • 28
    I don't understand why they in C# included a string.split(char) but not a string.split(string)... I mean there are both string.split(char[]) and string.split(string[])! – Johan Jan 20 '17 at 14:35
  • how to do it with the character `'` ? I tried this but it does not split – GuidoG Dec 08 '20 at 14:10
  • 1
    @GuidoG: If you have a problem with that then ask a new querstion on StackOverflow about it. – juergen d Dec 08 '20 at 14:34
  • @GuidoG Just checked it with the expression message.Split(new[] { "'" }, StringSplitOptions.None) - everything works fine – Paul WWJD Jan 12 '23 at 20:22
37
.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...

Anders Marzi Tornblad
  • 18,896
  • 9
  • 51
  • 66
21

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");
Huusom
  • 5,654
  • 1
  • 17
  • 15
15

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);
DanTheMan
  • 3,277
  • 2
  • 21
  • 40
13

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);
    }
  }
}
Patrick
  • 17,669
  • 6
  • 70
  • 85
12

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);
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Guillaume Slashy
  • 3,554
  • 8
  • 43
  • 68
5

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

Charles Lambert
  • 5,042
  • 26
  • 47