Sub 利用字典去重()
Dim aOld, aNew, Dic '声明数组、字典
Dim i As Long
Dim sOld, sNew As String '声明字符串
Set Dic = CreateObject("scripting.dictionary") '创建字典
sOld = "a b c 1 2 1 a c d" '旧字符
aOld = Split(sOld, " ") '将字符串分割后装入数组
For i = LBound(aOld) To UBound(aOld) '遍历数组中的元素
Dic(aOld(i)) = vbNullString '利用字典key的不重复性去重
Next i
aNew = Dic.keys '将字典的key装入数组
sNew = Join(aNew, " ") '将数组连接成字符串
Debug.Print "sOld:" & sOld
Debug.Print "sNew:" & sNew
End Sub
返回结果:
sOld:a b c 1 2 1 a c d
sNew:a b c 1 2 d