检查字典对词典

时间:2013-05-08 08:50:58

标签: vb.net string-comparison

我想要一种针对字典检查单词的方法(如果可能的话)。如果不能对字典进行检查,则检查单词列表(txt或excel表)。

Dim word As String

Console.WriteLine("Enter a message: ")
word = Console.ReadLine()

' If word <> dictionary Then
' Console.WriteLine("word in the dictionary")
' End If

1 个答案:

答案 0 :(得分:2)

您想知道单词是否在词典的键或值中?

If dictionary.ContainsKey(word) Then
    Console.WriteLine("Word in Dictionary-Key")
End If
If dictionary.ContainsValue(word) Then
    Console.WriteLine("Word in Dictionary-Value")
End If

(假设Dictionary(Of TKey, TValue)Dictionary(Of String, String)

ContainsKey是最有效的方法。如果你甚至想知道它是否是键或值的一部分,你必须使用循环或Linq(它也在内部使用循环):

If dictionary.Keys.Any(Function(k) k.Contains(word)) Then
    Console.WriteLine("Part of word in Dictionary-Key was word")
End If
If dictionary.Values.Any(Function(k) k.Contains(word)) Then
    Console.WriteLine("Part of word in Dictionary-Value was word")
End If