Convert a delimted string to a dictionary in C#

后端 未结 5 622
[愿得一人]
[愿得一人] 2020-12-24 11:30

I have a string of the format \"key1=value1;key2=value2;key3=value3;\"

I need to convert it to a dictionary for the above mentioned key value pairs.

What wou

5条回答
  •  北海茫月
    2020-12-24 12:20

    Updated Ani's to take in account the semi colon at the end. The where clause will ensure that you have a key and value before creating and entry.

    var dictionary = "key1=value1;key2=value2;key3=value3;"
        .Split(';')
        .Select (part  => part.Split('='))
        .Where (part => part.Length == 2)
        .ToDictionary (sp => sp[0], sp => sp[1]);
    

提交回复
热议问题