繁体   English   中英

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

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

如果我有一个像

string hello="HelloworldHellofriendsHelloPeople";

我想将其存储在这样的字符串中

Helloworld
Hellofriends
HelloPeople

找到字符串“ hello”时必须更改行

谢谢

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);

您可以使用此正则表达式

(?=Hello)

然后使用正则表达式的split方法分割字符串!

您的代码为:

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

您可以使用此代码-基于string.Replace

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

您可以使用string.split分割单词“ Hello”,然后将“ Hello”附加到字符串上。

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

这将提供您想要的输出

Helloworld
Hellofriends
HelloPeople

暂无
暂无

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

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