本文介绍了将定界的字符串转换为字典< string,uint>在C#中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何将字符串转换为Dictionary<string, uint>
?
该&已存在答案.我试过了,但这给了我错误.
There are already answers to this & I tried this but it gives me error.
string abc = "key1=value1,key2=value2";
下面的东西给出了错误:
And below thing gives error:
var dict = text.Split(new[] {','}, StringSplitOptions.RemoveEmptyEntries)
.Select(part => part.Split('='))
.ToDictionary(split => split[0], split => split[1]);
我不确定如何在字典中将值存储为uint
.有什么建议吗?
I'm not sure how to store the value as uint
in dictionary. Any suggestions?
更新:稍后,我想将此Dictionary<string, uint>
转换回逗号分隔的字符串(key=value
).如何使用linq进行反向转换?
Update: and at later point I want to convert this Dictionary<string, uint>
back to a comma-separated string (key=value
). How do I do that reverse conversion using linq?
推荐答案
您非常接近-您所需要做的就是解析uint
的值:
You are very close - all you need is to parse uint
for the value:
Dictionary<string,uint> dict = text.Split(new[] {','}, StringSplitOptions.RemoveEmptyEntries)
.Select(part => part.Split('='))
.ToDictionary(split => split[0], split => uint.Parse(split[1]));
这篇关于将定界的字符串转换为字典< string,uint>在C#中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!