Answer the question
In order to leave comments, you need to log in
Parsing a string in c#. How to implement?
There is a string like "key"="value";"key1"="value1";"key2"="value2"
We have a key string, we need to find what is contained after the first "=" and before ";"
That is, knowing the key, you need to parse its value?
Answer the question
In order to leave comments, you need to log in
the simplest option is to parse it into a map and take the value by key. And so - regulars.
const string input = "\"key\"=\"value\";\"key1\"=\"value1\";\"key2\"=\"value2\"";
var result = Regex.Matches(input, @"\""(?<key>\w*)\""=\""(?<value>\w*)\""")
.OfType<Match>()
.ToDictionary(match => match.Groups["key"], match => match.Groups["value"]);
foreach (var pair in result) {
Console.Write(pair.Key);
Console.Write("=");
Console.WriteLine(pair.Value);
}
string s = "\"key\"=\"value\";\"key1\"=\"value1\";\"key2\"=\"value2\"";
var dict = s.Split(';').Select(n => n.Split('=')).ToDictionary(v => v[0].Trim('\"'), v => v[1].Trim('\"'));
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question