0

How do I split a string at word not at a char, Like I want to split this string into an array of strings:

Hello /*End*/ World /*End*/ Bye /*End*/ live /*End*/

I want to split at /*End*/ so the array will be like this

{Hello,World,Bye,live}

Any suggestions?

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
Kamal Saeed
  • 105
  • 2
  • 11

6 Answers6

2

Use the string.Split(string[], StringSplitOptions) overload.

var parts = str.Split(new[] {@"/*End*/"}, StringSplitOptions.None)
juharr
  • 31,741
  • 4
  • 58
  • 93
1

You could use the regex class for this: (From System.Text.RegularExpressions namespace)

string[] Result = Regex.Split(Input, "end");

It gives you a string array that is splitted by the pattern you specified.

GeorgDangl
  • 2,146
  • 1
  • 29
  • 37
  • Do I have to import any thing to use this ? – Kamal Saeed Apr 07 '15 at 12:32
  • No, this comes with the .NET framework. Just add "using System.Text.RegularExpressions;" at the top of your source code file. – GeorgDangl Apr 07 '15 at 12:33
  • You can do quite a lot with regular expressions, you could start here:[Wikipedia RegEx article](http://en.wikipedia.org/wiki/Regular_expression) and find a lot of regex-syntax at this [CheatSheet](http://www.cheatography.com/davechild/cheat-sheets/regular-expressions/) – GeorgDangl Apr 07 '15 at 12:37
1

There is an overload Split function that takes an array of strings and you can give only 1 string, like below.

string input = "Hello /End/ World /End/ Bye /End/ live /End/ ";
var output = input.Split(new[] { "/*End*/" }, StringSplitOptions.None);
adricadar
  • 9,971
  • 5
  • 33
  • 46
1
        var str = @"Hello /End/ World /End/ Bye /End/ live /End/ ";

        var words = str.Split(new string[] { "/End/" }, System.StringSplitOptions.None);

        foreach(var word in words)
        {
            Console.WriteLine(word);    
        }
Adil Z
  • 121
  • 3
0

try Regex

  string input = "Hello /*End*/ World /*End*/ Bye /*End*/ live /*End*/";
  string pattern = "/*End*/";            // Split on hyphens 

  string[] substrings = Regex.Split(input, pattern);
blackmind
  • 1,286
  • 14
  • 27
0

Simple regex pattern:

\/\*End\*\/

see demo

GRUNGER
  • 486
  • 3
  • 14