【发布时间】:2017-05-10 06:20:44
【问题描述】:
我有一本映射到摩尔斯电码的所有字母的字典
Dictionary<string, string> data = new Dictionary<string, string>();
data.Add("a", ".-");
data.Add("b", "-...");
data.Add("c", "-.-.");
data.Add("d", "-..");
data.Add("e", ".");
data.Add("f", "..-.");
data.Add("g", "--.");
data.Add("h", "....");
data.Add("i", "..");
data.Add("j", ".---");
data.Add("k", "-.-");
data.Add("l", ".-..");
data.Add("m", "--");
data.Add("n", "-.");
data.Add("o", "---"); and so on..
我正在尝试检查字典中是否存在现有摩尔斯电码的子字符串。
foreach (var item in arraylist)
{
int smallcount=0;
int startIndex = 0;
//check if this combination exists for morse code
for(int w=0;w<shortInput;w++)
{
int substringLength=Convert.ToInt32(item[w].ToString());
string sub = morsecode.Substring(startIndex, substringLength);
if (data.ContainsValue(sub)) ;
{
smallcount++;
}
startIndex = startIndex + substringLength;
}
if(smallcount==shortInput)
{ count++; }
}
这里data.ContainsValue(sub) 总是返回true,即使字典中不存在该值。
如果我遗漏了什么,谁能告诉我。?
【问题讨论】:
标签: c# .net dictionary c#-4.0