[英]Split a string into String Array in c#
How do I split the string into a string array. 如何将字符串拆分为字符串数组。
My string looks like this 我的弦看起来像这样
string orgString = "1234-|@$@|-George,Michael -$@%@$-65489-|@$@|-Lawrence, Steve J -$@%@$-7897954-|@$@|-Oliver Mike -$@%@$-56465-|@$@|-Waldimir Tursoky";
Now I want my string array to store name and number along with -|@$@|-
现在,我希望我的字符串数组存储名称和数字以及
-|@$@|-
I tried the following code 我尝试了以下代码
string[] strArray = orgString.Split(new string[] {"-$@%@$-"}, StringSplitOptions.RemoveEmptyEntries);
My output looks like this: 我的输出如下所示:
"1234-|@$@|-George,Michael "
But my desired output is (name first, number last) 但是我想要的输出是(名字在前,数字在后)
"George,Michael -|@$@|-1234"
How can i achieve this in C#? 如何在C#中实现这一目标?
Just swap parts in the resulting string : 只需交换结果字符串中的部分即可:
string orgString = "1234-|@$@|-George,Michael -$@%@$-65489-|@$@|-Lawrence, Steve J -$@%@$-7897954-|@$@|-Oliver Mike -$@%@$-56465-|@$@|-Waldimir Tursoky";
string[] resultingList = orgString.Split(new string[] {"-$@%@$-"}, StringSplitOptions.RemoveEmptyEntries).Select(x=>x.Split(new string[] {"-|@$@|-"}, StringSplitOptions.RemoveEmptyEntries).Aggregate((x11,y)=>{return y+" -|@$@|- "+x11;})).ToArray();
foreach(string result in resultingList)
{
Console.WriteLine(result);
}
You can split again and then recombine 您可以再次拆分然后重新组合
string outerDivider = "-$@%@$-";
string innerDivider = "-|@$@|-";
var results = orgString
// Split by outer divider
.Split(new string[] { outerDivider }, StringSplitOptions.RemoveEmptyEntries)
// Then for each of the results, split again on the inner divider
.Select(x => x.Split(new string[] { innerDivider }, StringSplitOptions.RemoveEmptyEntries))
// Swap the order of elements around the inner divider and recombine into a string
.Select(x => string.Join(innerDivider, x[1], x[0]));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.