VBScript 变量可以不声明直接使用,如果这么做会有很大的弊端,所以还是建议强制声明为显示定义变量 ,可在文件开始处添加option Explicit
option Explicit
'1 绝对值
Dim MyNumber1
MyNumber1 = Abs(50.3) ' Returns 50.3.
MyNumber1 = Abs(-50.3) ' Returns 50.3.
'2 数组使用方法1
Dim A2
Dim B2
A2 = Array(10,20,30)
B2 = A2(2) ' B is now 30.
'3 数组使用方法2 注意此处比C++,java多了一个元素
dim fname3(5)
fname3(0) = "George"
fname3(1) = "John"
fname3(2) = "Thomas"
fname3(3) = "James"
fname3(4) = "Adrew"
fname3(5) = "Martin"
'4Asc函数返回与字符串的第一个字母对应的 ASCII 字符代码
Dim MyNumber4
MyNumber4 = Asc("A") ' Returns 65.
MyNumber4 = Asc("a") ' Returns 97.
MyNumber4 = Asc("Apple") ' Returns 65.
'5 将一个表达式转变成 Boolean类型,如果表达式是无效的则会产生异常
Dim A5, B5, Check5
A5 = 5: B5 = 5 ' Initialize variables.
Check5 = CBool(A5 = B5) ' Check contains True.
A5 = 0 ' Define variable.
Check5 = CBool(A5) ' Check contains False.
'Check5 = CBool("Hello world")‘产生异常操作
'6将表达式转换为字节(Byte)类型
Dim MyDouble6, MyByte6
MyDouble6 = 125.5678 ' MyDouble is a Double.
MyByte6 = CByte(MyDouble6) ' MyByte contains 126.
'7 CCur 函数可把表达式转换Currency类型
Dim MyDouble7, MyCurr7
MyDouble7 = 1234.214588 ' MyDouble is a Double.
MyCurr7 = CCur(MyDouble7) 'MyCurr7 = 1234.2146
'8 把acci马转化为字符
Dim MyChar8
MyChar8 = Chr(65) ' Returns A.
MyChar8 = Chr(97) ' Returns a.
MyChar8 = Chr(62) ' Returns >.
MyChar8 = Chr(37) ' Returns %.
'9 转化为interger类型
Dim MyDouble9, MyInt9
MyDouble9 = 2345.5678 ' MyDouble is a Double.
MyInt9 = CInt(MyDouble9) ' MyInt contains 2346.
'10 转化为字符串类型
Dim MyDouble, MyString
MyDouble = 437.324 ' MyDouble is a Double.
MyString = CStr(MyDouble) ' MyString contains "437.324".
'11
Dim MyVar11, MyCheck11
MyCheck11 = IsEmpty(MyVar11) ' Returns True.
MyVar11 = Null ' Assign Null.
MyCheck11 = IsEmpty(MyVar11) ' Returns False.
MyVar11 = Empty ' Assign Empty.
MyCheck11 = IsEmpty(MyVar11) ' Returns True.
'12
Dim MyVar12, MyCheck12
MyCheck12 = IsNull(MyVar12) ' Returns False.
MyVar12 = Null ' Assign Null.
MyCheck12 = IsNull(MyVar12) ' Returns True.
MyVar12 = Empty ' Assign Empty.
MyCheck12 = IsNull(MyVar12) ' Returns False.
'13
'Dim MyInt13, MyCheck13, MyObject13
'Set MyObject13 = CreateObject("Excel.Application")
'MyCheck13 = IsObject(MyObject13) ' Returns True.
'MyCheck13 = IsObject(MyInt13) ' Returns False.
'14 判断变量是不是数组
Dim MyVariable
Dim MyArray(3)
MyArray(0) = "Sunday"
MyArray(1) = "Monday"
MyArray(2) = "Tuesday"
MyVariable = IsArray(MyArray) ' MyVariable contains "True".
'15 获取数组的上写边界
Dim UboundNum, LboudNum
Dim a(10)
a(0)="Saturday"
a(1)="Sunday"
a(2)="Monday"
a(3)="Tuesday"
a(4)="Wednesday"
a(5)="Thursday"
UboundNum = UBound(a) '10
LboudNum = LBound(a) '0
'16 大小写转化
Dim MyString16
Dim UCaseString
Dim LCaseString16
MyString16 = "VBSCript"
LCaseString16 = LCase(MyString16) ' LCaseString contains "vbscript".
UCaseString = UCase(MyString16) 'UCaseString contains "VBSCRIPT"
'17 把数组合并成一个字符串
Dim MyString17
Dim MyArray17(3)
MyArray17(0) = 9
MyArray17(1) = "John "
MyArray17(2) = "Doe "
MyArray17(3) = "III"
MyString17 = Join(MyArray17) ' 9 contains "Mr. John Doe III".
'18 去掉空格
Dim MyVar
MyVar = LTrim(" vbscript ") ' MyVar contains "vbscript ".
MyVar = RTrim(" vbscript ") ' MyVar contains " vbscript".
MyVar = Trim(" vbscript ") ' MyVar contains "vbscript".
'19 获取子字符串
Dim MyVar19
MyVar19 = Mid("VBScript is fun!", 3, 6) ' MyVar contains "Script".
'20 字符串比较
Dim MyStr1, MyStr2, MyComp
MyStr1 = "ABCD": MyStr2 = "abcd" ' Define variables.
MyComp = StrComp(MyStr1, MyStr2, vbTextCompare) ' Returns 0.
MyComp = StrComp(MyStr1, MyStr2, vbBinaryCompare) ' Returns -1.
MyComp = StrComp(MyStr2, MyStr1) ' Returns
'21 字符串替换
Dim MyString21
' A binary comparison starting at the beginning of the string.
MyString21 = Replace("XXpXXPXXp", "p", "Y")
' Returns "XXYXXPXXY".
' A textual comparison starting at position 3.
MyString21 = Replace("XXpXXPXXp", "p", "Y", 3, -1, 1)
' Returns "YXXYXXY".