Most Useful VBScript Functions That You Must Know

In this article,I will discuss a few commonly used VBScript methods in UFT that you must know to write better optimized automation script in less time.So keep reading.

Useful VBScript Methods in UFT

Frquently Used VBScript Methods in UFT

InStr Function in VBscript

The InStr method returns the position of the first occurrence of one string within another.We can use this method to check wheter a string exists in a given String as well as compare two strings.

Syntax: InStr([start, ]string1, string2[, compare])

Arguments

start: It is an optional argument.It accepts numeric expression that sets the starting position for each search. If you don’t mention any value, search begins at the first character position. The start argument becomes mandatory if compare (the fourth argument) is specified .

string1
Required parameter. String expression being searched.

string2
Required parameter.String expression that needs to be sreached.

compare
It an optional parameter. Accepts Numeric value indicating the kind of comparison to use when evaluating substrings. Valid values are 0 and 1 for binary and textual comparision.You can also use constant values vbBinaryCompare and vbTextCompare in place of 0 and 1.Refer the following table.

ConstantValueDescription
vbBinaryCompare0Perform a binary comparison.
vbTextCompare1Perform a textual comparison.

Return Value

IfReturn Value
string1 is zero-length0
string1 is NullNull
string2 is zero-lengthstart
string2 is NullNull
string2 is not found0
string2 is found within string1Position at which match is found
start > Len(string2)0

Example

The following examples illustrates how to use InStr to search a string:

StrComp Function in VBscript

The StrComp method is used to compare two string and returns a value indicating the result of a string comparison.

Syntax: StrComp(string1, string2[, compare])

Arguments

string1
Required parameter. Any valid string expression.

string2
Required parameter. Any valid string expression.

compare
Optional parameter. Numeric value indicating the kind of comparison to use when evaluating strings. If the parameter not used, a binary comparison is performed. See the following table for valid values.

ConstantValueDescription
vbBinaryCompare0Perform a binary comparison.
vbTextCompare1Perform a textual comparison.

Return Value

The StrComp function has the following return values:

IfStrComp RETURN VALUE
string1 is zero-length-1
string1 is equal to string20
string1 is greater than string21
string1 or string2 is NullNull

Example

The following examples illustrates how to use StrComp for a string comparison.

LTrim; RTrim; and Trim Functions

The above three functions are used to return a copy of a string without leading spaces (LTrim), trailing spaces (RTrim), or both leading and trailing spaces (Trim).

Syntax: LTrim(string) RTrim(string) Trim(string)

Example

The following examples illustrates how to use LTrim,RTrim and Trim for removing spaces.

Left Function in VBscript

The Left function a specified number of characters from the left side of a string.

Syntax: Left(string, length)

Arguments

string
String expression from which the leftmost characters are returned. If string contains Null value, a Null value is returned.

length
Numeric expression indicating how many characters to return. If one provides 0, a zero-length string(“”) is returned. 

The following examples illustrates the use of Left function to return a specified number of characters from the left side of a string:

Right Function in VBscript

The Right function a specified number of characters from the right side of a string.

Syntax: Right(string, length)

Arguments

string
String expression from which the rightmost characters are returned. If string contains Null value, a Null value is returned.

length
Numeric expression indicating how many characters to return. If one provides 0, a zero-length string(“”) is returned. 

To determine the number of characters in a string, you can use the Len function.

Example

The following example illustrates how to use the Right function to return a specified number of characters from the right side of a string:

Len Function in VBscript

The Len function returns the number of characters in a string or the number of bytes required to store a variable.

Syntax: Len(string | varName)

Arguments

string or varName
Any valid string expression or variable name. If string or variable contains a Null, Null is returned.

Example:

The following example shows how to get the length of a string.

Mid Function in VBScript

The Mid function returns a specified number of characters from a given string.This a very useful method.We can use this method to retrieve certain number of characters from any postion in a string.

Syntax: Mid(string, start[, length])

Arguments

string
String expression from which characters are returned. If string contains Null, Null is returned.

start
Numeric value.Starting position from which characters should be retrieved. If start is greater than the number of characters in string, Mid returns a zero-length string (“”).

length
Optinal value.Number of characters to return. If not privided or if there are fewer than length characters in the text (including the character at start), all characters from the start position to the end of the string are returned.

To determine the number of characters in a string, you can use the Len function.

Example

The following example illustrates the uses of the Mid function.

Replace Function in VBscript

The Replace is mostly used to return a string in which a specified substring has been replaced with another substring.

Syntax: Replace(expression, find, replacewith[, start[, count[, compare]]])

Arguments

expression
Required. String expression containing substring to replace.

find
Required. Substring being searched for.

replacewith
Required. Replacement substring

start
Optional Value. Position within expression where substring search is to begin. If not provided, 1 is assumed. Must be used in conjunction with count.

count
Optional Value. Number of substring substitutions to perform. If not provided, the default value is -1, which means make all possible substitutions. Must be used in conjunction with start.

compare
Optional Value. Numeric value indicating the kind of comparison to use when evaluating substrings. Refer the following for valid values. If not provided, the default value is 0, which means perform a binary comparison.

ConstantValueDescription
vbBinaryCompare0Perform a binary comparison.
vbTextCompare1Perform a textual comparison.

Return Value

IfReplace Function returns
expression is zero-lengthZero-length string ("").
expression is NullAn error message.
find is zero-lengthCopy of expression.
replacewith is zero-lengthCopy of expression with all occurrences of find removed.
start > Len(expression)Zero-length string.
count is 0Copy of expression.

Example

The following example illustrates the uses of the Replace function to return a string after replacing required values.

StrReverse Function in VBscript

The StrReverse function returns a string in which the character order of a specified string is reversed.

Syntax: StrReverse(string1)

Example

The following example illustrates the StrReverse function to return a string in reverse order:

Space Function in VBscript

The Space function creates and return a string with the specified number of spaces.

Syntax: Space(number)

The number argument is the number of spaces you want in the string.

Example:

The following example shows how to use Space function.

CStr Function in VBscript

The CStr function is used to type cast a variant to a String.

Syntax: CStr(expression)

The expression argument is any valid expression.

Example:

The following example illustrates how to use the CStr function to convert a numeric value to a String:

CInt Function in VBscript

The CInt function is used to type cast a variant to an Integer.

Syntax: Cint(expression)

The expression argument is any valid expression.

Example:

The following example illustrates how to use the Cint function to convert a value to an Integer:

CDbl Function in VBscript

The CDbl function is used to type cast a variant to a Double.

Syntax: CDbl(expression)

The expression argument is any valid expression.

Example:

The following example illustrates how to use the CDbl function to convert a value to a Double:

CDate Function in VBscript

The CDate function is used to type convert a variant to a Date.

Syntax: CDate(expression)

The expression argument is any valid expression.

You can use the IsDate function to check whether an expression 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.

Example:

The following example illustrates how to use the CDate function to convert a value to a Date:

Abs Function in VBscript

The Abs function returns the absolute value of a number.

Syntax: Abs(number)

The absolute value of a number is its unsigned magnitude. For example, Abs(-1) and Abs(1) both return 1.

Example:

The following example illustrates how to use the Abs function to get the absolute value of a number irrespective of its sign.

Split Function in VBscript

The Split function returns one-dimensional array containing a specified number of substrings.This is also a very important method.

Syntax: Split(expression[, delimiter[, count[, compare]]])

Arguments

expression
Required parameter. String expression containing substrings and delimiters.

delimiter
Optional parameter. String character used to identify substring limits. If no value provided, the space character (” “) is assumed to be the delimiter.

count
Optional parameter. Number of substrings to be returned; -1 indicates that all substrings are returned.

compare
Optional parameter. Numeric value indicating the kind of comparison to use when evaluating substrings. Refer the following table for valid values.

ConstantValueDescription
vbBinaryCompare0Perform a binary comparison.
vbTextCompare1Perform a textual comparison.

Example:

The following example illustrates the uses of Split function to return an array from a string. The function performs a textual comparison of the delimiter, and returns all of the substrings.

UBound Function in VBscript

The UBound also known as Upper Bound of an array function is mostly used to get the length of the array.

Syntax:

Example:

The following example illustrates how to use Ubound function to get the length of the array.

Now Function in VBscript

The Now function returns the current date and time according to the setting of your computer’s system date and time.

The following example illustrates how to use Now function to get the current date and time.

In practicle scenario,this function is mostly used to gnerate unique file names to save screenshots in an automation framework.

DateAdd Function in VBscript

The DataAdd function allows us to add (or subtract) days, months, years, etc. to Date or Time.

Syntax: DateAdd(interval, number, date)

Arguments

interval
Required parameter. String expression that is the interval you want to add. Refer the below table for valid values.

number
Required parameter. Numeric expression that is the number of interval you want to add. The numeric expression can either be positive, for getting future date, or negative, for getting dates in the past.

date
Required parameter. Variant represents the date value to which interval is added.

The interval argument has the following settings:

SettingsDescription
yyyyYear
qQuarter
mMonth
yDay of year
dDay
wWeekday
wwWeek of year
hHour
nMinute
sSecond

Example:

The following example is adding 10 days to the current date

The Date function returns current date that has been used as parameter value in the above example.

DateDiff Function in VBscript

The DateDiff function is used to calculate number of days between two dates.

Syntax: DateDiff(interval, date1, date2)

Example:

The following example uses the DateDiff function to display the number of days between two given dates:

The interval argument settings has been given in DateAdd Function

DatePart Function in VBScript

The DatePart function is used get the specified part of a given date.

Syntax: DatePart(interval, date)

Example:

The following example uses the DatePart function to display the number of days between two given dates:

The interval argument settings has been given in DateAdd Function

Day Function in VBscript

The Day function in VBScript is used get day between 1 and 31 representing the day of the specified date.

The following example uses the Day function to get the value of day of a given date:

Concatenate Strings in VBScript

The & operator concatenates two strings in VBSript (VBA).The following example concatenates two strings Hello and World.The message box will display Hello World.

Conclusion

In this article,I have shared most frequently used VBScript functions in UFT.Hope you would have like this tutorial and find it helpful.If you have any queries please do mention them in the comment box and don’t forget to share this article.

Recommended Posts

Leave a Reply