244

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 and get This and a sentence.

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

gotqn
  • 42,737
  • 46
  • 157
  • 243
Saobi
  • 16,121
  • 29
  • 71
  • 81

10 Answers10

293

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
  • 47,767
  • 15
  • 98
  • 117
  • 7
    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 Jul 14 '09 at 17:53
  • 7
    This is just an example ... The point is: There is a String.Split that takes strings as delimiters. – bruno conde Jul 14 '09 at 17:57
  • 1
    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 Jul 14 '09 at 18:05
  • 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. – Oliver Friedrich Jul 14 '09 at 18:20
  • 3
    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 Jul 14 '09 at 18:46
  • 2
    @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 Jul 14 '09 at 19:41
56

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
  • 4,335
  • 1
  • 21
  • 22
  • @ 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". – ahawker Jul 14 '09 at 17:57
  • 1
    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 Jul 14 '09 at 18:03
  • Seems overly complicated as String.Splity allows you to split on a string already... – Ed S. Jul 14 '09 at 18:51
  • 3
    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 Jul 14 '09 at 18:58
  • note that the regex split method doesn't remove of pre delimiter and trailing delimiter on the variable... :( – melaos Oct 07 '11 at 01:53
37

Based on existing responses on this post, this simplify the implementation :)

namespace System
{
    public static class BaseTypesExtensions
    {
        /// <summary>
        /// Just a simple wrapper to simplify the process of splitting a string using another string as a separator
        /// </summary>
        /// <param name="s"></param>
        /// <param name="pattern"></param>
        /// <returns></returns>
        public static string[] Split(this string s, string separator)
        {
            return s.Split(new string[] { separator }, StringSplitOptions.None);
        }


    }
}
eka808
  • 2,257
  • 3
  • 29
  • 41
30
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.

ahawker
  • 3,306
  • 24
  • 23
8

...In short:

string[] arr = "This is a sentence".Split(new string[] { "is" }, StringSplitOptions.None);
ParPar
  • 7,355
  • 7
  • 43
  • 56
6

Or use this code; ( same : new String[] )

.Split(new[] { "Test Test" }, StringSplitOptions.None)
5

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.

Paul Sonier
  • 38,903
  • 3
  • 77
  • 117
4

Here is an extension function to do the split with a string separator:

public static string[] Split(this string value, string seperator)
{
    return value.Split(new string[] { seperator }, StringSplitOptions.None);
}

Example of usage:

string mystring = "one[split on me]two[split on me]three[split on me]four";
var splitStrings = mystring.Split("[split on me]");
SteveD
  • 819
  • 7
  • 13
0
var dict = File.ReadLines("test.txt")
               .Where(line => !string.IsNullOrWhitespace(line))
               .Select(line => line.Split(new char[] { '=' }, 2, 0))
               .ToDictionary(parts => parts[0], parts => parts[1]);


or 

    enter code here

line="to=xxx@gmail.com=yyy@yahoo.co.in";
string[] tokens = line.Split(new char[] { '=' }, 2, 0);

ans:
tokens[0]=to
token[1]=xxx@gmail.com=yyy@yahoo.co.in
Prabu
  • 41
  • 7
-5
string strData = "This is much easier"
int intDelimiterIndx = strData.IndexOf("is");
int intDelimiterLength = "is".Length;
str1 = strData.Substring(0, intDelimiterIndx);
str2 = strData.Substring(intDelimiterIndx + intDelimiterLength, strData.Length - (intDelimiterIndx + intDelimiterLength));
  • 19
    Before posting code, you should probably try running it through a compiler first, unless you denote that you haven't. There's a semicolon missing from the first line, and str1 & str2 are not defined. Not to mention that the code doesn't actually work how the OP wanted. This code splits it into "Th" and " is much easier". – mandreko Aug 18 '11 at 19:36