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,335,956
  • 823
  • 8,931
  • 9,049
Kamal Saeed
  • 105
  • 2
  • 10

6 Answers6

2

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

var parts = str.Split(new[] {@"/*End*/"}, StringSplitOptions.None)
juharr
  • 31,034
  • 4
  • 52
  • 91
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,026
  • 1
  • 27
  • 35
  • 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,523
  • 5
  • 31
  • 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
  • 26
0

Simple regex pattern:

\/\*End\*\/

see demo

GRUNGER
  • 486
  • 3
  • 13