0

我想要一种根据字典检查单词的方法(如果可能的话)。如果无法对照字典,请对照单词列表(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
4

1 回答 1

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)是 a 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
于 2013-05-08T08:55:26.123 回答