Not as easy as using split, however:
string input = "FirstName=ABC;LastName=XZY;Username=User1;Password=1234"; string username = Regex.Match(input, "Username=(?<username>.*?)(;|$)").Groups["username"].Value;
In this case, the groups can be in any order.
And if you like to make an announcement:
var answers = from tuple in input.Split(';') where tuple.StartsWith("Username=") select tuple.Split('=')[1]; username = answers.Count() > 0 ? answers.First() : string.Empty;
We can say that the last fragment has the best semantics.
EDIT: Update the last fragment to process input strings that do not have the required tuple.
Bruno brant
source share