如何将键/值添加到字典c# [英] How to add Key/Value to dictionary c#
问题描述
我有一个字符串Abc = {GRFD_Id = 29,GL_Id = 1,CK_Id = 15,CPM_Current_Float = 405012.1700,CGJM_Allowed_Float = 99999999.0000,WL_Name = RahnamCP,PK_Amount = 750.00,GK_Name = Domestic Diamond, GP_PK_Id = 51,VG_Expiry = 365,CP_Name = JohnClient}
来自字符串的样本
in GRFD_Id = 29
GRFD_Id是钥匙
29是价值
现在我也想要键和值进入字典..我怎么能实现这个目的!
一个非常强力的解决方案可能是使用字符串。拆分方法。类似于:
foreach ( string 元素 in originalstring.Split(' ,')){
myDictionary.Add(element.Split(' =')[< span class =code-digit> 0 ],element.Split(' =')[ 1 ]);
}
您可能:
- 删除标题和尾随花括号。
- 迭代通过使用
','
字符作为分隔符拆分剩余字符串而获得的数组。- 将数组添加到字典中获得的项目再次拆分该项目,这次使用
'='
字符作为分隔符。
string Abc = GRFD_Id = 29,GL_Id = 1,CK_Id = 15,CPM_Current_Float = 405012.1700,CGJM_Allowed_Float = 99999999.0000,WL_Name = RahnamCP,PK_Amount = 750.00,GK_Name =国内钻石,GP_PK_Id = 51,VG_Expiry = 365,CP_Name = JohnClient;
// 如果您只想将字符串转换为Dictionary,那么您可以关注代码。
var Dictionary = Abc.Split(&#39;,&#39;)。ToList()。选择(m =& gt;
{
var res = m.Split(&#39; =&#39;) ;
Dictionary& lt; string ,string& gt; dict = new Dictionary& lt; string ,string& gt;();
dict.Add(res [ 0 ], res [ 1 ]);
return dict;
});
// 如果想要将字符串的第一个值转换为字典,那么你可以以下代码。
var FirstRecord = Abc.Split(&#39;,&#39;)。ToList ()。选择(m =& gt;
{
var res = m.Split(&#39; =&# 39;);
字典& lt; 字符串,字符串& gt; dict = 新字典& ; lt; string ,string& gt;();
dict.Add(res [ 0 ],res [ 1 ]);
return dict;
}) .FirstOrDefault(); < / pre > < /跨度>集团kquote>
i have a string Abc = {GRFD_Id=29, GL_Id=1, CK_Id=15, CPM_Current_Float=405012.1700, CGJM_Allowed_Float=99999999.0000, WL_Name=RahnamCP, PK_Amount=750.00, GK_Name=Domestic Diamond, GP_PK_Id=51, VG_Expiry=365, CP_Name=JohnClient}
Sample from string
in GRFD_Id=29
GRFD_Id is the Key
29 is Value
now i want too the keys and values into dictionary .. how can i achieve this!解决方案A very brute force solution could be to use string.Split method. Something like:
foreach (string element in originalstring.Split(',')) { myDictionary.Add(element.Split('=')[0], element.Split('=')[1]); }
You might:
- remove the heading and trailing curly braces.
- iterate over the array obtained by splitting the remaining string using the
','
character as separator.- add to the dictionary the array items obtained splitting again the item, this time using the
'='
character as separator.
string Abc = "GRFD_Id=29, GL_Id=1, CK_Id=15, CPM_Current_Float=405012.1700, CGJM_Allowed_Float=99999999.0000, WL_Name=RahnamCP, PK_Amount=750.00, GK_Name=Domestic Diamond, GP_PK_Id=51, VG_Expiry=365, CP_Name=JohnClient"; //IF you simply want the string to converted into Dictionary then you can following code. var Dictionary = Abc.Split(',').ToList().Select(m => { var res = m.Split('='); Dictionary<string, string> dict = new Dictionary<string, string>(); dict.Add(res[0], res[1]); return dict; }); //and if want the first values of string to be converted into Dictionary then you can following code. var FirstRecord = Abc.Split(',').ToList().Select(m => { var res = m.Split('='); Dictionary<string, string> dict = new Dictionary<string, string>(); dict.Add(res[0], res[1]); return dict; }).FirstOrDefault();</pre>
这篇关于如何将键/值添加到字典c#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!