VBS基础篇 - Dictionary对象
生活随笔
收集整理的這篇文章主要介紹了
VBS基础篇 - Dictionary对象
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
VBS基礎篇 - Dictionary對象
Dictionary是存儲數(shù)據(jù)鍵和項目對的對象,其主要屬性有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"'字典中鍵值對數(shù)量 WScript.Echo "字典中現(xiàn)有鍵值對數(shù)量: " & 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值的數(shù)組For Counter = 0 To Dict.Count - 1 'Count返回Dictionary對象鍵數(shù)目 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 "字典中現(xiàn)有鍵值對數(shù)量: " & Dict.Count?
運行結(jié)果截圖:
posted on 2016-08-12 14:12 Ethon 閱讀(...) 評論(...) 編輯 收藏轉(zhuǎn)載于:https://www.cnblogs.com/wakey/p/5764737.html
總結(jié)
以上是生活随笔為你收集整理的VBS基础篇 - Dictionary对象的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。