Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# split, return key/value pairs in an array

Tags:

c#

database

split

I'm new to C#, and thus am looking for layman's terms regarding this. Essentially, what I would like to do is turn:

key1=val1|key2=val2|...|keyN=valN

into a database array where, you guessed it, key1 returns val1, key2 returns val2, etc. I know I could return a string using split, but from that point on, I'm at a loss. Any help would be greatly appreciated! I hope I've made my intentions clear, but if you have any questions, don't hesitate to ask!

like image 258
Josh Avatar asked Feb 13 '11 02:02

Josh


1 Answers

string s = "key1=val1|key2=val2|keyN=valN";
var dict = s.Split('|')
            .Select(x => x.Split('='))
            .ToDictionary(x => x[0], x => x[1]);

Now dict is a Dictionary<string, string> with the desired key/value pairs.

like image 161
jason Avatar answered Oct 03 '22 23:10

jason