VBScript for Automation (QTP/UFT) Testing – Part 4
In VBScript – Part 4, let’s see the following topics:
STRING FUNCTIONS:
Let’s see the syntax and example of some of the important String Functions:
UCase:
Converts a specified string to uppercase
Syntax:
1 |
UCase(string) |
Example:
1 2 3 4 |
dim str str = " Software Testing Material " msgbox str msgbox ucase(str) |
Lcase:
Converts a specified string to lowercase
Syntax:
1 |
LCase(string) |
Example:
1 2 3 |
str = " Software Testing Material " msgbox str msgbox lcase(str) |
Len:
Returns the number of characters of a string
Syntax:
1 |
Len(string | varname) |
Example:
1 2 3 4 |
dim str str = " Software Testing Material " msgbox str msgbox len(str) |
Replace:
Replaces a specified part of a string with another string a specified number of times
Syntax:
1 |
Replace(expression, find, replacewith[, start[, count[, compare]]]) |
Example:
1 2 3 4 |
dim str str = " Software Testing Material " msgbox str msgbox replace(str, "A", "A", 1, 2,1) |
Space:
Returns a string that consists of a specified number of spaces
Syntax:
1 |
Space(number) |
Example:
1 2 3 4 |
dim str str = " Software Testing Material " msgbox str msgbox str & space(10) & str |
Left:
Returns a specified number of characters from the left side of a string
Syntax:
1 |
Left(string, length) |
Example:
1 2 3 4 |
dim str str = " Software Testing Material " msgbox str msgbox left(str, 5) |
Right:
Returns a specified number of characters from the right side of a string
Syntax:
1 |
Right(string, length) |
Example:
1 2 3 4 |
dim str str = " Software Testing Material " msgbox str msgbox right(str, 5) |
Mid:
Returns a specified number of characters from a string
Syntax:
1 |
Mid(string, start[, length]) |
Example:
1 2 3 4 |
dim str str = " Software Testing Material " msgbox str msgbox mid(str, 5, 9) |
Trim:
Removes spaces on both the left and the right side of a string
Syntax:
1 |
Trim(string) |
Example:
1 2 3 4 |
dim str str = " Software Testing Material " msgbox str msgbox len(trim(str)) |
LTrim:
Removes spaces on the left side of a string
Syntax:
1 |
LTrim(string) |
Example:
1 2 3 4 |
dim str str = " Software Testing Material " msgbox str msgbox len(ltrim(str)) |
RTrim:
Removes spaces on the right side of a string
Syntax:
1 |
RTrim(string) |
Example:
1 2 3 4 |
dim str str = " Software Testing Material " msgbox str msgbox len(rtrim(str)) |
StrReverse:
It reverses a string
Syntax:
1 |
StrReverse(string) |
Example:
1 2 3 4 |
dim str str = " Software Testing Material " msgbox str msgbox strreverse(str) |
InStr:
Returns the position of the first occurrence of one string within another
Syntax:
1 |
InStr([start, ]string1, string2[, compare]) |
Example:
1 2 3 4 |
dim str str = " Software Testing Material " msgbox str msgbox instr(1,str, "Testing",1) |
InStrRev:
Returns the position of the first occurrence of one string within another. The search begins at the last character of the string
Syntax:
1 |
InStrRev(string1, string2[, start[, compare]]) |
Example:
1 2 3 4 |
dim str str = " Software Testing Material " msgbox str msgbox instrrev(str, "Testing", len(str),1) |
StrComp:
Compares two strings and returns a value that represents the result of the comparison
Syntax:
1 |
StrComp(string1, string2[, compare]) |
Example:
1 2 3 4 |
dim str str = " Software Testing Material " msgbox str msgbox strcomp(str, "Testing",1) |
Split:
It returns an array that contains a specific number of values split based on a delimiter
Syntax:
1 |
Split(expression[, delimiter[, count[, compare]]]) |
Example:
1 2 3 4 5 6 7 |
dim str str = " Software Testing Material " msgbox str A=split(str) for i =0 to ubound(A) msgbox A(i) next |
DATE & TIME FUNCTIONS:
Let’s see the syntax and example of some of the important Date & Time Functions:
Now:
Returns the current system date and time
Syntax:
1 |
Now |
Example:
1 |
msgbox now |
Date:
Returns the current system date
Syntax:
1 |
Date |
Example:
1 |
msgbox date |
Time:
Returns the current system time
Syntax:
1 |
Time |
Example:
1 |
msgbox time |
DateAdd:
Returns a date to which a specified time interval has been added
Syntax:
1 |
DateAdd(interval, number, date) |
Example:
1 |
msgbox DateAdd("YYYY", 1, now) |
DateDiff:
Returns the number of intervals between two specified dates
Syntax:
1 |
DateDiff(interval, date1, date2 [,firstdayofweek[,firstweekofyear]]) |
Example:
1 |
msgbox DateDiff("YYYY", now, dateadd("YYYY", 1, now)) |
DatePart:
Returns the specified part of a given date
Syntax:
1 |
DatePart(interval, date[, firstdayofweek[, firstweekofyear]]) |
Example:
1 |
msgbox DatePart("YYYY",now) |
Day:
Returns a number that represents the day of the month
Syntax:
1 |
Day(date) |
Example:
1 |
msgbox day(now) |
Month:
Returns a number that represents the month of the year
Syntax:
1 |
Month(date) |
Example:
1 |
msgbox month(now) |
MonthName:
Returns the name of a specified month
Syntax:
1 |
MonthName(month[, abbreviate]) |
Example:
1 |
msgbox monthname(month(now)) |
Weekday:
Returns a number that represents the day of the week
Syntax:
1 |
Weekday(date, [firstdayofweek]) |
Example:
1 |
msgbox weekday(now) |
WeekdayName:
Returns the weekday name of a specified day of the week
Syntax:
1 |
WeekdayName(weekday, abbreviate, firstdayofweek) |
Example:
1 |
msgbox weekdayname(weekday(now)) |
Year:
Returns a number that represents the year
Syntax:
1 |
Year(date) |
Example:
1 |
msgbox year(now) |
Hour:
Returns a number that represents the hour of the day
Syntax:
1 |
Hour(time) |
Example:
1 |
msgbox hour(time) |
Minute:
Returns a number that represents the minute of the hour
Syntax:
1 |
minute(time) |
Example:
1 |
msgbox<span style="line-height: 1.5;"> minute(time)</span> |
Second:
Returns a number that represents the second of the minute
Syntax:
1 |
Second(time) |
Example:
1 |
msgbox second(time) |
TimeSerial:
Returns the time for a specific hour, minute, and second
Syntax:
1 |
TimeSerial(hour, minute, second) |
Example:
1 |
msgbox timeserial(2,00,1) |
TimeValue:
Returns a time
Syntax:
1 |
TimeValue(time) |
Example:
1 |
msgbox timevalue(time) |
VBScript Series:
VBScript for Automation (QTP/UFT) Testing – Part 1
VBScript for Automation (QTP/UFT) Testing – Part 2
VBScript for Automation (QTP/UFT) Testing – Part 3
VBScript for Automation (QTP/UFT) Testing – Part 4
VBScript for Automation (QTP/UFT) Testing – Part 5