简体   繁体   中英

C# joining strings after a specific character

I have a string and would like to know how I can split the values into separate variables

string c = "1237^^^John^^^Abraham^^Dr";

I would like to put Dr. John Abraham into one variable.

String name = "Dr. John Abraham";

Would be grateful for advice on this. I tried the indexof but couldn't figure out correctly.

You can use String.Split with ^ as the separator and RemoveEmptyEntries

var input = "1237^^^John^^^Abraham^^Dr";
var split = input.Split(new[]{'^'}, StringSplitOptions.RemoveEmptyEntries);
Console.WriteLine("{0}. {1} {2}", split[3], split[1], split[2]);

Live example: http://rextester.com/HHE60664

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.

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