VBS基础篇 - Dictionary对象
生活随笔
收集整理的這篇文章主要介紹了
VBS基础篇 - Dictionary对象
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
VBS基礎(chǔ)篇 - Dictionary對(duì)象
Dictionary是存儲(chǔ)數(shù)據(jù)鍵和項(xiàng)目對(duì)的對(duì)象,其主要屬性有Count、Item、Key,主要方法有Add、Exists、Items、Keys、Remove、RemoveAll。
'建立字典 Dim Dict : Set Dict = CreateObject("Scripting.Dictionary")'添加鍵值對(duì) Dict.Add "Key1", "Item1" Dict.Add "Key2", "Item2" Dict.Add "Key3", "Item3"'字典中鍵值對(duì)數(shù)量 WScript.Echo "字典中現(xiàn)有鍵值對(duì)數(shù)量: " & Dict.Count '讓一個(gè)腳本在屏幕上顯示文本信息 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返回一個(gè)包含所有Item值的數(shù)組For Counter = 0 To Dict.Count - 1 'Count返回Dictionary對(duì)象鍵數(shù)目 WScript.Echo _"鍵: " & DictKeys(Counter) & _ '& 字符串連接運(yùn)算符"值: " & DictItems(Counter)Next End SubTraverseDictWScript.Echo '在一個(gè)鍵值對(duì)中,修改鍵或修改值 Dict.Key("Key2") = "Keyx" Dict.Item("Key1") = "Itemx" TraverseDictWScript.Echo '刪除指定鍵 Dict.Remove("Key3") TraverseDictWScript.Echo '刪除全部鍵 Dict.RemoveAll WScript.Echo "字典中現(xiàn)有鍵值對(duì)數(shù)量: " & Dict.Count?
運(yùn)行結(jié)果截圖:
posted on 2016-08-12 14:12 Ethon 閱讀(...) 評(píng)論(...) 編輯 收藏轉(zhuǎn)載于:https://www.cnblogs.com/wakey/p/5764737.html
總結(jié)
以上是生活随笔為你收集整理的VBS基础篇 - Dictionary对象的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。