I have this string
string sx="(colorIndex=3)(font.family=Helvetica)(font.bold=1)";
and am splitting it with
string [] ss=sx.Split(new char[] { '(', ')' },
StringSplitOptions.RemoveEmptyEntries);
Instead of that, how could I split the result into a Dictionary<string,string>
? The
resulting dictionary should look like:
Key Value
colorIndex 3
font.family Helvetica
font.bold 1
You could do this with regular expressions:
Randal Schwartz has a rule of thumb: use split when you know what you want to throw away or regular expressions when you know what you want to keep.
You know what you want to keep:
With a little effort, you can do it with
ToDictionary
:Not sure whether this looks nicer:
You can try
Often used for http query splitting.
I am just putting this here for reference...
For ASP.net, if you want to parse a string from the client side into a dictionary this is handy...
Create a JSON string on the client side either like this:
or like this:
or even like this:
pass it to the server...
then parse it on the server side like this:
JavaScriptSerializer requires System.Web.Script.Serialization.