简体   繁体   English

如何分割字符串并存储在不同的字符串数组中

[英]How to split a string and store in different string array

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" 找到字符串“ hello”时必须更改行

thanks 谢谢

string hello = "HelloworldHellofriendsHelloPeople";
var a = hello.Split(new string[] { "Hello"}, StringSplitOptions.RemoveEmptyEntries);
foreach (string s in a)
    Console.WriteLine("Hello" + s);
 var result = hello.Split(new[] { "Hello" }, 
                    StringSplitOptions.RemoveEmptyEntries)
               .Select(s => "Hello" + s);

You can use this regex 您可以使用此正则表达式

(?=Hello)

and then split the string using regex's split method! 然后使用正则表达式的split方法分割字符串!

Your code would be: 您的代码为:

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

You can use this code - based on string.Replace 您可以使用此代码-基于string.Replace

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

You could use string.split to split on the word "Hello", and then append "Hello" back onto the string. 您可以使用string.split分割单词“ Hello”,然后将“ Hello”附加到字符串上。

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

That will give the output you want of 这将提供您想要的输出

Helloworld
Hellofriends
HelloPeople

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM