VBS基础篇 - Dictionary对象
Dictionary是存储数据键和项目对的对象,其主要属性有Count、Item、Key,主要方法有Add、Exists、Items、Keys、Remove、RemoveAll。
'建立字典 Dim Dict : Set Dict = CreateObject("Scripting.Dictionary")'添加键值对 Dict.Add "Key1", "Item1" Dict.Add "Key2", "Item2" Dict.Add "Key3", "Item3"'字典中键值对数量 WScript.Echo "字典中现有键值对数量: " & Dict.Count '让一个脚本在屏幕上显示文本信息 WScript.Echo '检查指定键是否存在 If Dict.Exists("Key1") ThenWScript.Echo "Key1 存在!" ElseWScript.Echo "Key1 不存在!" End IfIf Dict.Exists("Keyn") ThenWScript.Echo "Keyn 存在!" ElseWScript.Echo "Keyn 不存在!" End IfWScript.Echo '遍历字典 Sub TraverseDictDim DictKeys, DictItems, CounterDictKeys = Dict.KeysDictItems = Dict.Items 'Items返回一个包含所有Item值的数组For Counter = 0 To Dict.Count - 1 'Count返回Dictionary对象键数目 WScript.Echo _"键: " & DictKeys(Counter) & _ '& 字符串连接运算符"值: " & DictItems(Counter)Next End SubTraverseDictWScript.Echo '在一个键值对中,修改键或修改值 Dict.Key("Key2") = "Keyx" Dict.Item("Key1") = "Itemx" TraverseDictWScript.Echo '删除指定键 Dict.Remove("Key3") TraverseDictWScript.Echo '删除全部键 Dict.RemoveAll WScript.Echo "字典中现有键值对数量: " & Dict.Count
运行结果截图:
posted on 2016-08-12 14:12 Ethon 阅读(...) 评论(...) 编辑 收藏