837

I've been using the Split() method to split strings, but this only appears to work if you are splitting a string by a character. Is there a way to split a string, with another string being the split by parameter?

I've tried converting the splitter into a character array, with no luck.

In other words, I'd like to split the string:

THExxQUICKxxBROWNxxFOX

by xx, and return an array with values:

THE, QUICK, BROWN, FOX

Ghasem
  • 14,455
  • 21
  • 138
  • 171
Brandon
  • 8,743
  • 2
  • 20
  • 16
  • 6
    For future concerns: One of the below comment interested me so I decided to open a [discussion](https://softwareengineering.stackexchange.com/q/372293/307733) on software engineering concerning the _nonintuitive_ (but right) way to do it in the accepted answer. – scharette Jun 14 '18 at 14:33

11 Answers11

1499

In order to split by a string you'll have to use the string array overload.

string data = "THExxQUICKxxBROWNxxFOX";

return data.Split(new string[] { "xx" }, StringSplitOptions.None);
Adam Robinson
  • 182,639
  • 35
  • 285
  • 343
  • 4
    I actually ended up changing my answer to this for 2 reasons: #1: To handle the splits I want to do I would need to use Regex.Escape, because my split string will often contain asterisks, etc. #2: While this program I'm writing needs no real optimization, there does appear to be additional overhead involved with using the Regex Split method. – Brandon Feb 11 '10 at 15:42
  • 1
    In my defense, Jon Skeet suggested somewhere to use Regex when splitting strings with strings as a seperator, and I have to admit when he suggest sth. I tend to believe it seldom is bad advice. ('Although chances are you want split by string rather than character, in which case you'll want to look atRegex.Split') – Peter Feb 11 '10 at 16:25
  • 7
    @Peter: In that post Jon is suggesting it because the poster does not have a fixed delimiter; he is looking to split strings separated by "more than one space" (meaning 2+). For strings delimited by a *pattern* rather than a *value*, RegEx is a great (well, the *only*) option. For fixed-value delimiters, it introduces needless overhead. Try running a test; as the number of operations increases, RegEx ends up taking somewhere around ~10x as long as a corresponding `string.Split`. – Adam Robinson Feb 11 '10 at 16:31
  • 9
    I come from Python to C#. Python supports string split by another string. And I frequently need to come back to this question for a simple answer to `string[] Split(string pattern)`, which is the most natural usage I could think of yet it isn't there. I wrote C before so I am used to char arrays but I still hate to see `char[]` popping up in a C# code because it suddenly drags my attention from stream level to byte level. Anybody know why C# library guys designed the Split method like this? If there is a good reason, I can probably try to appreciate it despite the inconvenience. – foresightyj Jan 23 '15 at 05:24
  • 18
    This snippet ranks very high on the the list of things I'd be ashamed of to show to non C# developers. – ASA May 09 '15 at 22:51
  • 158
    Why the hell can't we just do `data.Split("xx")`? – mcont Jun 18 '15 at 10:12
  • You could save some characters and shut ReSharper up by doing this : `data.Split(new [] { "xx" }, StringSplitOptions.None);`. If this is used often it might be worth creating your own split method. Something like `public static string[] Split(this string text, string[] seperator) { return text.Split(seperator, StringSplitOptions.None); }` – hatsrumandcode Aug 02 '18 at 10:51
  • @mcont We can split by string in .net core. [Check this answer](https://stackoverflow.com/a/56284791/1248177) – aloisdg Sep 25 '19 at 15:18
  • @mcont I try this but it not work it must be char – Mostafa Anssary Feb 11 '21 at 13:45
  • Even with the understanding that having both `Split(char)` and `Split(string)` makes `Split(null)` ambiguous, why then not use a distinct method name? Have `Split(char)` + family and `Split_S(string)` or whatever + family and let them eat cake – Moige Aug 31 '23 at 09:07
156

edit: See @Danation's answer for newer/less versbose overload


There is an overload of Split that takes strings.

"THExxQUICKxxBROWNxxFOX".Split(new [] { "xx" }, StringSplitOptions.None);

You can use either of these StringSplitOptions

  • None - The return value includes array elements that contain an empty string
  • RemoveEmptyEntries - The return value does not include array elements that contain an empty string

So if the string is "THExxQUICKxxxxBROWNxxFOX", StringSplitOptions.None will return an empty entry in the array for the "xxxx" part while StringSplitOptions.RemoveEmptyEntries will not.

Greg
  • 16,540
  • 9
  • 51
  • 97
  • It does not quite "take" strings. It expects an array of chars, you simply used the literal constructor for this. – Sven Mawby Dec 16 '20 at 12:46
  • 2
    @SvenMawby Nah, it "literally" has an "overload" for an "array" of "strings". `Split(String[], StringSplitOptions)` – Greg Mar 07 '22 at 02:34
91
Regex.Split(string, "xx")

is the way I do it usually.


Of course you'll need:

using System.Text.RegularExpressions;

or :

System.Text.RegularExpressions.Regex.Split(string, "xx")

but then again I need that library all the time.

T.Todua
  • 53,146
  • 19
  • 236
  • 237
Peter
  • 47,963
  • 46
  • 132
  • 181
  • 16
    @Brandon: While I'm usually cautioning against premature optimization, you should be aware that a `RegEx.Split` is quite a bit more costly than a simple `String.Split` because of the regular expression overhead. – Adam Robinson Feb 11 '10 at 15:33
  • 11
    If you want to split by an arbitrary string, use `Regex.Escape` on the string first, this will escape any regex meta-characters. – Richard Feb 11 '10 at 15:38
  • one of the key advantages that may pay for overhead is ability to provide string comparison setting – Timur Sadykov Apr 24 '14 at 19:29
52

There's an overload of String.Split for this:

"THExxQUICKxxBROWNxxFOX".Split(new [] {"xx"}, StringSplitOptions.None);
bruno conde
  • 47,767
  • 15
  • 98
  • 117
31

I generally like to use my own extension for that:

string data = "THExxQUICKxxBROWNxxFOX";
var dataspt = data.Split("xx");
//>THE  QUICK  BROWN  FOX 


//the extension class must be declared as static
public static class StringExtension
{   
    public static string[] Split(this string str, string splitter)
    {
        return str.Split(new[] { splitter }, StringSplitOptions.None);
    }
}

This will however lead to an Exception, if Microsoft decides to include this method-overload in later versions. It is also the likely reason why Microsoft has not included this method in the meantime: At least one company I worked for, used such an extension in all their C# projects.

It may also be possible to conditionally define the method at runtime if it doesn't exist.

Lorenz Lo Sauer
  • 23,698
  • 16
  • 85
  • 87
  • 4
    Alternatively, use `params string[] splitter` as the second parameter and change `new[] {splitter}` to `splitter` to support multiple delimiters. – Matthew Strawbridge Jun 03 '14 at 20:53
18

As of .NET Core 2.0, there is an override that takes a string.

So now you can do "THExxQUICKxxBROWNxxFOX".Split("xx").

See https://learn.microsoft.com/en-us/dotnet/api/system.string.split?view=netcore-2.0#System_String_Split_System_String_System_StringSplitOptions_

Danation
  • 743
  • 8
  • 20
17

The previous answers are all correct. I go one step further and make C# work for me by defining an extension method on String:

public static class Extensions
{
    public static string[] Split(this string toSplit, string splitOn) {
        return toSplit.Split(new string[] { splitOn }, StringSplitOptions.None);
    }
}

That way I can call it on any string in the simple way I naively expected the first time I tried to accomplish this:

"a big long string with stuff to split on".Split("g str");
Matt
  • 25,467
  • 18
  • 120
  • 187
argyle
  • 1,319
  • 2
  • 14
  • 28
7
string data = "THExxQUICKxxBROWNxxFOX";

return data.Replace("xx","|").Split('|');

Just choose the replace character carefully (choose one that isn't likely to be present in the string already)!

SNag
  • 17,681
  • 10
  • 54
  • 69
  • 2
    @MasoudHosseini: Please read the complete answer; there's already a disclaimer. – SNag Mar 05 '15 at 09:36
  • 3
    @kobe: Because it's a terrible hack. – Overv Jun 22 '15 at 10:39
  • 3
    Works fine, but it is dangerous for generic methods – Kaizonaro Feb 24 '16 at 16:52
  • 8
    Posting explanations like, "It's a terrible hack" or "a bad answer" are not helpful. It's simply an opinion without explanation. Instead, stating something like "It's unnecessary to both scan the string for replacements and then scan for split characters since it leads to poor performance." would be a better way to explain yourself. Too many programmers act this way. :( – Matt Ruwe Dec 20 '16 at 11:23
  • 2
    What if the string contains the `|` char already, for this reason I think it's dangerous to use. – amd Jan 28 '18 at 12:12
  • 1
    @amd: The answer is a template, and doesn't necessarily enforce the use of `|`. I hope you read the footnote disclaimer and the rest of the comments here. – SNag Jan 29 '18 at 07:35
  • 1
    I'm using this XD, too bad .NET didn't have overload for single string split problem. – Kokizzu Apr 20 '18 at 12:35
2

Create this function first.

string[] xSplit(string str, string sep) {
    return str.Split(new [] {sep}, StringSplitOptions.None);
}

Then use it like this.

xSplit("THExxQUICKxxBROWNxxFOX", "xx");
Mohammad
  • 41
  • 3
-2

This is also easy:

string data = "THExxQUICKxxBROWNxxFOX";
string[] arr = data.Split("xx".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
user890255
  • 458
  • 5
  • 9
-5

The easiest way is to use String.Replace:

string myString = "THExxQUICKxxBROWNxxFOX";
mystring = mystring.Replace("xx", ", ");

Or more simply:

string myString = "THExxQUICKxxBROWNxxFOX".Replace("xx", ", ");
Cole Tobin
  • 9,206
  • 15
  • 49
  • 74
user3458227
  • 93
  • 1
  • 1
  • 10
  • 3
    As it is, this won't return an array (as the question asks for), just a string with commas where the `xx`'s were. – Arj Sep 08 '14 at 15:44
  • And not only that if the string contained additional comma's you would not be able to split out the words correctly. – user3658298 May 13 '15 at 13:57
  • He is onto something though. If you also chain it with a split. Doubt it is effective, but it is more readable.. var myStrings = "THExxQUICKxxBROWNxxFOX".Replace("xx", "|").Split('|'); – Terje Solem Jan 05 '21 at 09:07
  • @Terje. What if there are already some "|" in the start string ? – frenchone Aug 17 '21 at 16:01