P
P
p4p2014-10-07 19:48:04
C++ / C#
p4p, 2014-10-07 19:48:04

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

4 answer(s)
S
Sergey, 2014-10-07
Protko @Fesor

the simplest option is to parse it into a map and take the value by key. And so - regulars.

G
GavriKos, 2014-10-07
@GavriKos

Do you happen to have json in there? Can you use his parse?

I
Ilya Glebov, 2014-10-08
@IljaGlebov

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);
}

M
Mikhail Andreev, 2014-10-13
@Mikant

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 question

Ask a Question

731 491 924 answers to any question