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:
UCase(string)
Example:
dim str str = " Software Testing Material " msgbox str msgbox ucase(str)
Lcase:
Converts a specified string to lowercase
Syntax:
LCase(string)
Example:
str = " Software Testing Material " msgbox str msgbox lcase(str)
Len:
Returns the number of characters of a string
Syntax:
Len(string | varname)
Example:
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:
Replace(expression, find, replacewith[, start[, count[, compare]]])
Example:
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:
Space(number)
Example:
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:
Left(string, length)
Example:
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:
Right(string, length)
Example:
dim str str = " Software Testing Material " msgbox str msgbox right(str, 5)
Mid:
Returns a specified number of characters from a string
Syntax:
Mid(string, start[, length])
Example:
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:
Trim(string)
Example:
dim str str = " Software Testing Material " msgbox str msgbox len(trim(str))
LTrim:
Removes spaces on the left side of a string
Syntax:
LTrim(string)
Example:
dim str str = " Software Testing Material " msgbox str msgbox len(ltrim(str))
RTrim:
Removes spaces on the right side of a string
Syntax:
RTrim(string)
Example:
dim str str = " Software Testing Material " msgbox str msgbox len(rtrim(str))
StrReverse:
It reverses a string
Syntax:
StrReverse(string)
Example:
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:
InStr([start, ]string1, string2[, compare])
Example:
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:
InStrRev(string1, string2[, start[, compare]])
Example:
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:
StrComp(string1, string2[, compare])
Example:
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:
Split(expression[, delimiter[, count[, compare]]])
Example:
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:
Now
Example:
msgbox now
Date:
Returns the current system date
Syntax:
Date
Example:
msgbox date
Time:
Returns the current system time
Syntax:
Time
Example:
msgbox time
DateAdd:
Returns a date to which a specified time interval has been added
Syntax:
DateAdd(interval, number, date)
Example:
msgbox DateAdd("YYYY", 1, now)
DateDiff:
Returns the number of intervals between two specified dates
Syntax:
DateDiff(interval, date1, date2 [,firstdayofweek[,firstweekofyear]])
Example:
msgbox DateDiff("YYYY", now, dateadd("YYYY", 1, now))
DatePart:
Returns the specified part of a given date
Syntax:
DatePart(interval, date[, firstdayofweek[, firstweekofyear]])
Example:
msgbox DatePart("YYYY",now)
Day:
Returns a number that represents the day of the month
Syntax:
Day(date)
Example:
msgbox day(now)
Month:
Returns a number that represents the month of the year
Syntax:
Month(date)
Example:
msgbox month(now)
MonthName:
Returns the name of a specified month
Syntax:
MonthName(month[, abbreviate])
Example:
msgbox monthname(month(now))
Weekday:
Returns a number that represents the day of the week
Syntax:
Weekday(date, [firstdayofweek])
Example:
msgbox weekday(now)
WeekdayName:
Returns the weekday name of a specified day of the week
Syntax:
WeekdayName(weekday, abbreviate, firstdayofweek)
Example:
msgbox weekdayname(weekday(now))
Year:
Returns a number that represents the year
Syntax:
Year(date)
Example:
msgbox year(now)
Hour:
Returns a number that represents the hour of the day
Syntax:
Hour(time)
Example:
msgbox hour(time)
Minute:
Returns a number that represents the minute of the hour
Syntax:
minute(time)
Example:
msgbox minute(time)
Second:
Returns a number that represents the second of the minute
Syntax:
Second(time)
Example:
msgbox second(time)
TimeSerial:
Returns the time for a specific hour, minute, and second
Syntax:
TimeSerial(hour, minute, second)
Example:
msgbox timeserial(2,00,1)
TimeValue:
Returns a time
Syntax:
TimeValue(time)
Example:
msgbox timevalue(time)
I would like to conclude VBScript – Part 4 here and will start VBScript – Part 5 in the next post.
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