私はC#のnoobieです。ContainsKey()
がbid_amount
とbid_amount
の値でtrueを返さない理由を理解できません。辞書ContainsKey()は文字列の値を比較しません
私は長い文字列("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] _ – Steve
Worksの周りにキー/値のペアを追加する前にトリムを追加することができます。あなたの入力を確認してください。有効な入力を持つコードの実際の例はこちらを参照してください。https://dotnetfiddle.net/SxOJ8M –