0

任何帮助都会非常感谢。

例如,我有一个字符串

Dim words As String = "Hello world i fell like badword"
Dim inputCheck As String = words.Split(New Char() {" "c})

带有单词和替换单词的哈希表

您将如何遍历 hastable 并替换匹配的单词?

For Each Item In hsTable

                Next

结果将是“Hello world i fall like * ” 坏词被替换为哈希表中的替换词匹配。

谢谢你。

4

2 回答 2

0

Pretty straightforward.

dim result as new list(of string) 
for each word in inputCheck
  result.Add if(hsTable.Keys.Contains(word), hsTable(word), word)
next
dim replaced = string.Join(" ", result)
于 2013-05-16T19:05:04.330 回答
0

您使用哈希表的任何特殊原因?使用 KeyValuePair 列表很容易做到这一点。像这样的东西会起作用吗?

Dim words As String = "Hello world i fell like badword"

Dim newWords As New List(Of KeyValuePair(Of String, String))
newWords.Add(New KeyValuePair(Of String, String)("badword", "goodword"))
newWords.Add(New KeyValuePair(Of String, String)("badword2", "goodword2"))

newWords.ForEach(Function(w) words.Replace(w.Key, w.Value))
于 2013-05-16T19:35:02.803 回答