tags:

views:

3928

answers:

5

What if I want to split a string using a delimiter that is a word?

For example, "This is a sentence".

I want to split on "is". So I will get "This " and " a sentence".

In Java, I can send in a string as a delimiter, but how do I accomplish this in C#?

+8  A: 

You can use the Regex.Split method, something like this:

Regex regex = new Regex(@"\bis\b");
string[] substrings = regex.Split("This is a sentence");

foreach (string match in substrings)
{
   Console.WriteLine("'{0}'", match);
}

Edit: This satisfies the example you gave. Note that an ordinary String.Split will also split on the "is" at the end of the word "This", hence why I used the Regex method and included the word boundaries around the "is". Note, however, that if you just wrote this example in error, then String.Split will probably suffice.

IRBMe
@ EDIT: I wasn't sure either, but you can still use the normal string split and just pad spaces on either side if his goal is to ONLY remove the word "is".
Hawker
That doesn't work either (at least not without a lot more effort), because you don't know whether the space should go on the left, the right or both without knowing the positions of the word that was split on in the string.
IRBMe
Seems overly complicated as String.Splity allows you to split on a string already...
Ed Swangren
I've already addressed this in my edit to my answer, in the comment above and in 3 comments on another answer. *String.Split* does not work for the example provided in the question. I'm not going to repeat myself yet again by explaining why. You can read all of the other comments if you wish to know.
IRBMe
+8  A: 

http://msdn.microsoft.com/en-us/library/system.string.split.aspx

Example from the docs:

string source = "[stop]ONE[stop][stop]TWO[stop][stop][stop]THREE[stop][stop]";
string[] stringSeparators = new string[] {"[stop]"};
string[] result;

// ...
result = source.Split(stringSeparators, StringSplitOptions.None);

foreach (string s in result)
{
    Console.Write("'{0}' ", String.IsNullOrEmpty(s) ? "<>" : s);
}
bruno conde
This actually returns:'Th' ' ' ' a sentence'with the example given in the question. Perhaps this is what he actually wants, but it's not what he specified.
IRBMe
This is just an example ... The point is: There is a String.Split that takes strings as delimiters.
bruno conde
Yes, but it doesn't do what the question specifies. You have to use something a bit more clever to get the output specified. Now, whether what the question specified is actually what the asker *wants* is a different question, but the question asked here can't be answered trivially with just String.Split.
IRBMe
This is a bit more clever, he just would need to change the delimiter string to " is " (include the whitespace) and even the unclever example works fine.
BeowulfOF
Still doesn't quite work. If you include the spaces in the word to split, they're not included in the output. If you examine the example given in the question, you'll notice that they do in fact include the spaces. Splitting on " is " would give you "This" and "a sentence" rather than "This " and " a sentence". Note the subtle spaces at the end of "This" and beginning of "a sentence". Again, this answer is probably what the questioner actually wants, but it's not what he asked and, I repeat, *String.Split* won't trivially solve it.
IRBMe
@IRBMe have you read the question? 'I want to split on "is". So I will get "This " and " a sentence"'. See the spaces in the results??? This is exacly what Split does.
bruno conde
+3  A: 

The String.Split method allows you to split strings using string or character delimiters, among other things.

Steve Guidi
RTFM isn't what the user was asking for.
Michael K Campbell
A: 

You can use String.Replace() to replace your desired split string with a character that does not occur in the string and then use String.Split on that character to split the resultant string for the same effect.

McWafflestix
+2  A: 
string s = "This is a sentence.";
string[] res = s.Split(new string[]{ " is " }, StringSplitOptions.None);

for(int i=0; i<res.length; i++)
    Console.Write(res[i]);

EDIT: The "is" is padded on both sides with spaces in the array in order to preserve the fact that you only want the word "is" removed from the sentence and the word "this" to remain intact.

Hawker
Of course this breaks for the sentence "It is what it is."
JohnFx