VB Scripting Imp Functions
Array Function
Returns a Variant containing an array.
Array(arglist)
The required arglist argument is a comma-delimited list of values that are assigned to the elements of an array contained with the Variant. If no arguments are specified, an array of zero length is created.
Remarks
The notation used to refer to an element of an array consists of the variable name followed by parentheses containing an index number indicating the desired element. In the following example, the first statement creates a variable named A. The second statement assigns an array to variable A. The last statement assigns the value contained in the second array element to another variable.
Dim A
A =Array(
10,
20,
30)
B = A(2) ' B is now 30.
Note A variable that is not declared as an array can still contain an array. Although a Variant variable containing an array is conceptually different from an array variable containing Variant elements, the array elements are accessed in the same way.
CDate Function
Returns an expression that has been converted to a Variant of subtype Date.
CDate(date)
The date argument is any valid date expression.
Remarks
Use the IsDate function to determine if date can be converted to a date or time. CDate recognizes date literals and time literals as well as some numbers that fall within the range of acceptable dates. When converting a number to a date, the whole number portion is converted to a date. Any fractional part of the number is converted to a time of day, starting at midnight.
CDate recognizes date formats according to the locale setting of your system. The correct order of day, month, and year may not be determined if it is provided in a format other than one of the recognized date settings. In addition, a long date format is not recognized if it also contains the day-of-the-week string.
The following example uses the CDate function to convert a string to a date. In general, hard coding dates and times as strings (as shown in this example) is not recommended. Use date and time literals (such as #10/19/1962#, #4:45:23 PM#) instead.
MyDate = "October 19, 1962" ' Define date.
MyShortDate =CDate(
MyDate)
' Convert to Date data type.
MyTime = "4:35:47 PM" ' Define time.
MyShortTime =CDate(
MyTime)
' Convert to Date data type.
Chr Function
Returns the character associated with the specified ANSI character code.
Chr(charcode)
The charcode argument is a number that identifies a character.
Remarks
Numbers from 0 to 31 are the same as standard, nonprintable ASCII codes. For example, Chr(10) returns a linefeed character.
The following example uses the Chr function to return the character associated with the specified character code:
Dim MyChar
MyChar =Chr(
65)
' Returns A.
MyChar =Chr(
97)
' Returns a.
MyChar =Chr(
62)
' Returns >.
MyChar =Chr(
37)
' Returns %.
Note The ChrB function is used with byte data contained in a string. Instead of returning a character, which may be one or two bytes, ChrB always returns a single byte. ChrW is provided for 32-bit platforms that use Unicode characters. Its argument is a Unicode (wide) character code, thereby avoiding the conversion from ANSI to Unicode.
IsObject Function
Returns a Boolean value indicating whether an expression references a valid Automation object.
IsObject(expression)
The expression argument can be any expression.
Remarks
IsObject returns True if expression is a variable of Object subtype or a user-defined object; otherwise, it returns False.
The following example uses the IsObject function to determine if an identifier represents an object variable:
Dim MyInt, MyCheck, MyObject
Set MyObject = Me
MyCheck =IsObject(
MyObject)
' Returns True.
MyCheck =IsObject(
MyInt)
' Returns False.
Join Function
Returns a string created by joining a number of substrings contained in an array.
Join(list[, delimiter])
Arguments
list
Required. One-dimensional array containing substrings to be joined.
delimiter
Optional. String character used to separate the substrings in the returned string. If omitted, the space character (" ") is used. If delimiter is a zero-length string, all items in the list are concatenated with no delimiters.
Remarks
The following example uses the Join function to join the substrings of MyArray
:
Dim MyString
Dim MyArray(3)
MyArray(0) = "Mr."
MyArray(1) = "John "
MyArray(2) = "Doe "
MyArray(3) = "III"
MyString =Join(
MyArray)
' MyString contains "Mr. John Doe III".
LBound Function
Returns the smallest available subscript for the indicated dimension of an array.
LBound(arrayname[, dimension])
Arguments
arrayname
Name of the array variable; follows standard variable naming conventions.
dimension
Whole number indicating which dimension's lower bound is returned. Use 1 for the first dimension, 2 for the second, and so on. If dimension is omitted, 1 is assumed.
Remarks
The LBound function is used with the UBound function to determine the size of an array. Use the UBound function to find the upper limit of an array dimension.
The lower bound for any dimension is always 0.
LCase Function
Returns a string that has been converted to lowercase.
LCase(string)
The string argument is any valid string expression. If string contains Null, Null is returned.
Remarks
Only uppercase letters are converted to lowercase; all lowercase letters and non-letter characters remain unchanged.
The following example uses the LCase function to convert uppercase letters to lowercase:
Dim MyString
Dim LCaseString
MyString = "VBSCript"
LCaseString =LCase(
MyString)
' LCaseString contains "vbscript".
Left Function
Returns a specified number of characters from the left side of a string.
Left(string, length)
Arguments
string
String expression from which the leftmost characters are returned. If string contains Null, Null is returned.
length
Numeric expression indicating how many characters to return. If 0, a zero-length string("") is returned. If greater than or equal to the number of characters in string, the entire string is returned.
Remarks
To determine the number of characters in string, use the Len function.
The following example uses the Left function to return the first three characters of MyString
:
Dim MyString, LeftString
MyString = "VBSCript"
LeftString =Left(
MyString, 3)
' LeftString contains "VBS".
Note The LeftB function is used with byte data contained in a string. Instead of specifying the number of characters to return, length specifies the number of bytes.
LTrim; RTrim; and Trim Functions
Returns a copy of a string without leading spaces (LTrim), trailing spaces (RTrim), or both leading and trailing spaces (Trim).
LTrim(string)
RTrim(string)
Trim(string)
The string argument is any valid string expression. If string contains Null, Null is returned.
Remarks
The following example uses the LTrim, RTrim, and Trim functions to trim leading spaces, trailing spaces, and both leading and trailing spaces, respectively:
Dim MyVar
MyVar = LTrim(
" vbscript ")
' MyVar contains "vbscript ".
MyVar = RTrim(
" vbscript ")
' MyVar contains " vbscript".
MyVar = Trim(
" vbscript ")
' MyVar contains "vbscript".
Right Function
Returns a specified number of characters from the right side of a string.
Right(string, length)
Arguments
string
String expression from which the rightmost characters are returned. If string contains Null, Null is returned.
length
Numeric expression indicating how many characters to return. If 0, a zero-length string is returned. If greater than or equal to the number of characters in string, the entire string is returned.
Remarks
To determine the number of characters in string, use the Len function.
The following example uses the Right function to return a specified number of characters from the right side of a string:
Dim AnyString, MyStr
AnyString = "Hello World" ' Define string.
MyStr =Right(
AnyString,
1)
' Returns "d".
MyStr =Right(
AnyString,
6)
' Returns " World".
MyStr =Right(
AnyString,
20)
' Returns "Hello World".
Note The RightB function is used with byte data contained in a string. Instead of specifying the number of characters to return, length specifies the number of bytes.
Split Function
Returns a zero-based, one-dimensional array containing a specified number of substrings.
Arguments
expression
Required. String expression containing substrings and delimiters. If expression is a zero-length string,
delimiter
Optional. String character used to identify substring limits. If omitted, the space character (" ") is assumed to be the delimiter. If delimiter is a zero-length string, a single-element array containing the entire expression string is returned.
count
Optional. Number of substrings to be returned; -1 indicates that all substrings are returned.
compare
Optional. Numeric value indicating the kind of comparison to use when evaluating substrings. See Settings section for values.
Settings
The compare argument can have the following values:
Constant | Value | Description |
vbBinaryCompare | 0 | Perform a binary comparison. |
vbTextCompare | 1 | Perform a textual comparison. |
Remarks
The following example uses the
Dim MyString, MyArray, Msg
MyString = "VBScriptXisXfun!"
MyArray =
' MyArray(0) contains "VBScript".
' MyArray(1) contains "is".
' MyArray(2) contains "fun!".
Msg = MyArray(0) & " " & MyArray(1)
Msg = Msg & " " & MyArray(2)
MsgBox Msg
Sqr Function
Returns the square root of a number.
Sqr(number)
The number argument can be any valid numeric expression greater than or equal to 0.
Remarks
The following example uses the Sqr function to calculate the square root of a number:
Dim MySqr
MySqr =Sqr(
4)
' Returns 2.
MySqr =Sqr(
23)
' Returns 4.79583152331272.
MySqr =Sqr(
0)
' Returns 0.
MySqr =Sqr(
-4)
' Generates a run-time error.
StrComp Function
Returns a value indicating the result of a string comparison.
StrComp(string1, string2[, compare])
Arguments
string1
Required. Any valid string expression.
string2
Required. Any valid string expression.
compare
Optional. Numeric value indicating the kind of comparison to use when evaluating strings. If omitted, a binary comparison is performed. See Settings section for values.
Settings
The compare argument can have the following values:
Constant | Value | Description |
vbBinaryCompare | 0 | Perform a binary comparison. |
vbTextCompare | 1 | Perform a textual comparison. |
Return Values
The StrComp function has the following return values:
If | StrComp returns |
string1 is less than string2 | -1 |
string1 is equal to string2 | 0 |
string1 is greater than string2 | 1 |
string1 or string2 is Null | Null |
Remarks
The following example uses the StrComp function to return the results of a string comparison. If the third argument is 1, a textual comparison is performed; if the third argument is 0 or omitted, a binary comparison is performed.
Dim MyStr1, MyStr2, MyComp
MyStr1 = "ABCD": MyStr2 = "abcd" ' Define variables.
MyComp = StrComp(MyStr1, MyStr2, 1) ' Returns 0.
MyComp = StrComp(MyStr1, MyStr2, 0) ' Returns -1.
MyComp = StrComp(MyStr2, MyStr1) ' Returns 1.
StrReverse Function
Returns a string in which the character order of a specified string is reversed.
StrReverse(string1)
The string1 argument is the string whose characters are to be reversed. If string1 is a zero-length string (""), a zero-length string is returned. If string1 is Null, an error occurs.
Remarks
The following example uses the StrReverse function to return a string in reverse order:
Dim MyStr
MyStr =StrReverse(
"VBScript")
' MyStr contains "tpircSBV".
Reference: HP QTP 9.5 Help
0 comments:
Post a Comment