c# - String to dictionary, and back again -
i working string this:
norway, true; sweden, false; england, null; denmark, false;
i'm trying dictionary<string, bool?>
can work it, remove items, compare against other stuff. when i'm done, want convert dictionary similar string , save it.
any ideas?
you can convert dictionary using split
method , linq:
var dict = str.split(';') .select(s => s.split(',')) .todictionary( p => p[0].trim() , p => p[1].trim().equals("null") ? null : (bool?)(bool.parse(p[1].trim())) );
converting easier:
var res = string.join("; ", dict.select( p => string.format( "{0}, {1}" , p.key , p.value.hasvalue ? p.value.tostring().tolowercase() : "null" ) ));
Comments
Post a Comment