任何帮助都会非常感谢你。
我有一个字符串例如
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
out come将是“Hello world我喜欢 * ”badword被替换为哈希表中的替换单词匹配。
谢谢。答案 0 :(得分:0)
非常简单。
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)
答案 1 :(得分:0)
您使用Hashtable的任何特殊原因?使用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))