【发布时间】:2016-08-02 05:29:59
【问题描述】:
我是 C# 的菜鸟,不明白为什么 ContainsKey() 不会在值 bid_amount 和 bid_amount 时返回 true。
我有一个长字符串("key=value, key=value..."),它在, 上拆分,然后= 创建到字典中。但是,当我在字典 bid_amount 中测试已知键时,它总是返回 false。
有没有办法使用 ContainsKey() 比较值? 我到底错过了什么?
string[] responseStringSplit = responseString.Split (new []{ ',' });
Dictionary<string,string> responseDict = new Dictionary<string, string> ();
foreach (string word in responseStringSplit)
{
string[] splitString = word.Split (new[]{ ':' });
if (splitString.Length > 1)
{
responseDict.Add(splitString [0],splitString [1]);
}
else
{
Debug.Log ("CouldNotSplit:" + word);
}
}
foreach (KeyValuePair<string, string> entry in responseDict)
{
Debug.Log (entry.Key + "=" + entry.Value);
if (responseDict.ContainsKey ("bid_amount"))
{
Debug.Log ("found bid amount");
}
}
【问题讨论】:
-
你能告诉我们你的输入字符串吗?
-
你确定有这样的字符串吗?我的意思是,它周围没有空间?也许您可以在添加 spitstring[0], [1] 周围的键/值对之前添加 Trim
-
为我工作。检查您的输入。请参阅此处以获取具有有效输入的代码的工作示例:dotnetfiddle.net/SxOJ8M
标签: c# dictionary split