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#?
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]));
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.