views:

312

answers:

4

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 any 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.

+8  A: 

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
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
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 splitby string rather than character, in which case you'll want to look atRegex.Split')
Peter
@Peter: I'm not sure I understand the message of your comment.
Adam Robinson
Okay, I edited it
Peter
@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
Ok, I see, tx for the info. I must admit I never needed a string.split in a performance sensitive environment.
Peter
+1  A: 
Regex.Split(string,"xx")

is the way I do it usually. Of cours you'll need a

using System.Text.RegularExpressions;

but than again : I need that lib all the time.

Peter
Other answers worked, but I felt that this was easiest.
Brandon
@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
If you want to split by an arbitrary string, use `Regex.Escape` on the string first, this will escape any regex meta-characters.
Richard
+3  A: 

There is an overload of Split that takes strings.

"THExxQUICKxxBROWNxxFOX".Split(new string[] { "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
+1  A: 

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

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