c# - Create a dictionary from a semicolon separated string using linq -



c# - Create a dictionary<string,string> from a semicolon separated string using linq -

so have string like:

"some key:some value; john:doe;age:234"

i wrote method takes string , returns a:

dictionary<string,string>

was curious if via linq?

assuming delimiters cannot appear in keys or values:

var dict = str.split(';') .select(s => s.split(':')) .todictionary(a => a[0].trim(), => a[1].trim()));

this not fastest way it, simplest.

you utilize regex:

static readonly regex parser = new regex(@"([^:]):([^;])"); var dict = parser.getmatches(str) .cast<match>() .todictionary(m => m.groups[0].value.trim(), m => m.groups[0].value.trim() );

c# linq

Comments

Popular posts from this blog

iphone - Dismissing a UIAlertView -

intellij idea - Update external libraries with intelij and java -

javascript - send data from a new window to previous window in php -