VBS数组在应用中没有像其他语句那么广泛,VBS数组存在不少功能上的局限性(如二维数组的定义、赋值),在使用上也没有java等语言那么便捷。下面来具体讲解下几个数组函数的使用方式:
arr = array("t1","t2")
MsgBox arr(0)
MsgBox arr(1)
IsArray 函数
IsArray(varname)
说明
下面的示例利用
Dim MyVariable
Dim MyArray(3)
MyArray(0) = "Sunday"
MyArray(1) = "Monday"
MyArray(2) = "Tuesday"
MyVariable = IsArray(MyArray) ' MyVariable 包含 "True"。
UBound 函数
UBound(arrayname[, dimension])
参数
说明
Dim A(100,3,4)
语句 | 返回值 |
UBound(A, 1) | 100 |
UBound(A, 2) | 3 |
UBound(A, 3) | 4 |
LBound 函数
LBound(arrayname[, dimension])
参数
说明
arr = array("t1","t2","t3")
For i=0 To UBound(arr)-LBound(arr)
MsgBox arr(i)
Next
Split 函数
Split(expression[, delimiter[, count[, start]]])
参数
设置
常数 | 值 | 描述 |
vbBinaryCompare | 0 | 执行二进制比较。 |
vbTextCompare | 1 | 执行文本比较。 |
说明
MyString = "VBScriptXisXfun!"
MyArray = Split(MyString, "x", -1, 1)
' MyArray(0) 包含 "VBScript"。
' MyArray(1) 包含 "is"。
' MyArray(2) 包含 "fun!"。
Msg = MyArray(0) & " " & MyArray(1)
Msg = Msg & " " & MyArray(2)
MsgBox Msg
再如:
stng = "t1,t2,t3"
arr = Split(stng,",")
MsgBox arr(0)
MsgBox arr(1)
MsgBox arr(2)
Join 函数
Join(list[,delimiter])
参数
Dim MyArray(3)
MyArray(0) = "Mr."
MyArray(1) = "John "
MyArray(2) = "Doe "
MyArray(3) = "III"
MyString = Join(MyArray) 'MyString 包含 "Mr. John Doe III"。
再如:
arr = array("t1","t2","t3")
MsgBox Join(arr,",")
例一:一维数组的比较
Dim b
a=Array(10,15,30)
b=Array(10,20,30)
flag=1
For i=0 To UBound(a)-LBound(a)
If a(i)=b(i) Then
flag=1
Else
flag=0
n=UBound(a)
msgbox "数组a"&n&":="&a(i)&","&"数组b"&n&":="&b(i)
End If
Next
例二:二维数组的比较
array1(0,0)=1
array1(0,1)=2
array1(1,0)=3
array1(1,1)=4
Dim array2(1,1)
array2(0,0)=1
array2(0,1)=2
array2(1,0)=4
array2(1,1)=4
flag=1
For i=0 To 1
For j=0 To 1
If array1(i,j)=array2(i,j) Then
flag=1
Exit for
Else
flag=0
msgbox "array1"&"("&i&","&j&")"&"="&array1(i,j)&","&"array2"&"("&i&","&j&")"&"="&array2(i,j)
End If
Next
Next
例三:使用循环来比较数组里是否包含某值
arr = array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, "a", "b", "c", "d", "f", "g")
str = "a"
For i = 0 To UBound(arr)
If arr(i) = str Then
Exit For
end if
Next
If i <= UBound(arr) Then
msgbox "arr中包含str的值!"
Else
msgbox "arr中不包含str的值!"
End If
例四:使用函数组合来比较数组里是否包含某值(优化逻辑)
arr = array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, "a", "b", "c", "d", "f", "g")
str = "a"
If InStr(Join(arr, "|"), str) > 0 Then
msgbox "arr中包含str的值!"
Else
msgbox "arr中不包含str的值!"
End If
例五:VBS中数组作为函数的返回值
Dim myarray(2)
Dim aa, bb, cc
Dim myvalue, bbbase, ccbase
bbbase=array("Beijing", "NewYork", "Copenhagen", "Paris", "London", "Gothenborg")
ccbase=array("China", "America", "Denmark", "Franch", "England", "Sweden")
'Get a number contains 8 characters
aa= Int((99999999 - 11111111+ 1) * Rnd + 11111111)
' Get a number between 1 to 6
myvalue=Int((6 * Rnd) + 1)
bb=bbbase(myvalue)
cc=ccbase(myvalue)
myarray(0)=CStr(aa)
myarray(1)=bb
myarray(2)=cc
GenerateRandom=myarray
End Function
'****************************************************
' Call the function
Dim testarr
testarr=GenerateRandom
msgbox testarr(0)
msgbox testarr(1)
msgbox testarr(2)
例六:数组排序
Dim oArrayList, iElement
Set oArrayList = CreateObject( "System.Collections.ArrayList" )
For iElement = 0 To UBound(aSortThisArray)
oArrayList.Add aSortThisArray(iElement)
Next
oArrayList.Sort
set fSortArray = oArrayList
End Function
myarray=Array(50,20,30)
MsgBox myarray(0)
MsgBox fSortArray(myarray)(0)
'CreateObject( "System.Collections.ArrayList" )调用了mscoree.dll,是.NET Framework相关组件。