1

if i have a string like

string hello="HelloworldHellofriendsHelloPeople";

i would like to store this in a string like this

Helloworld
Hellofriends
HelloPeople

It has to change the line when it finds the string "hello"

thanks

1
  • 3
    Do you want a string, or an array? Commented Sep 21, 2012 at 18:12

5 Answers 5

6
string hello = "HelloworldHellofriendsHelloPeople";
var a = hello.Split(new string[] { "Hello"}, StringSplitOptions.RemoveEmptyEntries);
foreach (string s in a)
    Console.WriteLine("Hello" + s);
Sign up to request clarification or add additional context in comments.

Comments

4
 var result = hello.Split(new[] { "Hello" }, 
                    StringSplitOptions.RemoveEmptyEntries)
               .Select(s => "Hello" + s);

3 Comments

You beat me into it... hahaha! anyway, I think this is the best answer but since the OP is asking to store it into a string array, adding .ToArray() at the end will do that.
@pinoy_ISF The OP never asked for the result to be an array.
@Servy The title says "How to split a string and store in different string array" ;-)
2

You can use this regex

(?=Hello)

and then split the string using regex's split method!

Your code would be:

      String matchpattern = @"(?=Hello)";
      Regex re = new Regex(matchpattern); 
      String[] splitarray = re.Split(sourcestring);

Comments

0

You can use this code - based on string.Replace

var replace = hello.Replace( "Hello", "-Hello" );
var result = replace.Split("-");

3 Comments

in this case she does contain (string hello="HelloworldHellofriendsHelloPeople"; ), Please you can read question before comment
it's easy to replace with another separator, it's not difficult, and just for this you set downvote ????
The OP isn't looking for code to parse just that one example input. If he was then you could just return new[]{"Helloworld"},"Hellofriends",...}. Clearly that's not useful. Even if you change the separator, the code will always break if the input string contains that separator. Here the actual delimiter is Hello, and the code should act accordingly.
0

You could use string.split to split on the word "Hello", and then append "Hello" back onto the string.

string[] helloArray = string.split("Hello");
foreach(string hello in helloArray)
{
    hello = "Hello" + hello;
}

That will give the output you want of

Helloworld
Hellofriends
HelloPeople

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.