This tutorial has been prepared for the beginners to help them understand basic-to-advanced functionality of VBScript.
In this tutorial we have discussed mainly the widely used string functions in Vb script with example.
1. VBScript in QTP
Microsoft VBScript (Visual Basic Script) is a general-purpose, lightweight , scripting language developed
by Microsoft which is modeled on Visual Basic. VBScript is the main scripting language for Quick Test
Professional (QTP).
As Vb Script is Lightweight language, there is no need to compile the program, during execution they
got interpreted and run. No .exe file got generated.
Script engines like Wscript.exe & Cscript.exe interpret VBScript file in Windows GUI environment
using Windows script Host. WSH is the system module that transforms a VBScript file into a Windows
executable file. Wscript.exe is used to display output and receive input in Windows GUI format such as
dialog and input boxes. Cscript.exe is used in a command-line environment.
VBScript Variable
In VBScript all variables are of the type variant that can store any type of value.
It supports int, long, double, String, Boolean ,date datatypes. A variant behaves as a number when it is
used in a numeric context and as a string when used in a string context.
Variables can be declared using the following statements that determines the scope of the variable.
The scope of the variable plays a crucial role when used within a procedure or classes.
ñDim --Variables declared with Dim at the script level are available to all procedures within the script.
At the procedure level, variables are available only within the procedure.
ñPublic Variables declared using "Public" Keyword are available to all the procedures across all the
associated scripts.
ñPrivate --Variables that are declared as "Private" have scope only within that script in which they are
declared.
VB Script String Functions:
Len Function : Returns the number of characters in a string
Example:
Str="Welcome to the QTP Learning Blog"
Msgbox Len(Str)
' Output --> 32
Msgbox Len("Mindfire Solutions")
' Output--> 18
2. LCase Function : Returns a string that has been converted to lowercase.
Example:
Str="Welcome to the QTP Learning Blog"
Msgbox LCase(Str)
' Output --> welcome to the qtp learning blog
Msgbox LCase("Mindfire Solutions")
' Output--> mindfire solutions
UCase Function : Returns a string that has been converted to uppercase.
Str="Welcome to the QTP Learning Blog"
Msgbox UCase(Str)
' Output --> WELCOME TO QTP LEARNING BLOG
Msgbox UCase("Mindfire Solutions")
' Output--> MINDFIRE SOLUTIONS
Left Function :Returns a specified number of characters from the left side of a string.
Str="Welcome to the QTP Learning Blog"
Msgbox LEFT(Str,7)
' Output --> Welcome
Msgbox LEFT("Mindfire Solutions",2)
' Output--> Mi
second parameter is 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.
Msgbox LEFT(Str,40)
' Output --> WELCOME TO QTP LEARNING BLOG
3. Msgbox LEFT("Mindfire Solutions",0) O/P—blank msgbox
Right Function : Returns a specified number of characters from the right side of a string.
Str="Welcome to the QTP Learning Blog"
Msgbox Right(Str,7)
' Output --> ng Blog
Msgbox Right("Mindfire Solutions",2)
' Output--> ns
second parameter is 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.
Msgbox Right(Str,40)
' Output --> WELCOME TO QTP LEARNING BLOG
Msgbox Right("Mindfire Solutions",0) O/P—blank msgbox
Mid Function : Returns a specified number of characters from a string.
Str="Welcome to the QTP Learning Blog"
Msgbox Mid(Str,3,4)
' Output --> lcom
Msgbox Mid("Mindfire Solutions",2,5)
' Output--> indfi
Msgbox Mid("Mindfire Solutions",2)
' Output--> indfire Solutions
Second parameter, in string at which the part to be taken begins. If start is greater than the number
of characters in string, Mid returns a zero-length string ("") which is blank.
Third parameter, Number of characters to return. If omitted , all characters from the start position to
the end of the string are returned.
Replace Function : It Returns a string in which a specified substring has been replaced with another
4. substring a specified number of times.
Str="Welcome to the QTP Learning Blog"
print Replace(Str, "to","into")
'Output --> Welcome into the QTP Learning Blog
Print Replace("Mindfire Solutions","Solutions","Org")
'Output --> Mindfire Org
Print Replace("Quick Test ProfeSsional","s","x",5,3,0)
'Output --> k Text ProfeSxional
Print Replace("Quick Test ProfeSsional","s","x",5,3,1)
'Output --> k Text Profexxional
Second parameter, in string is which specified substring has been replaced
Third parameter, is the Replacement substring.
Fourth, Fifth and Sixth parameters are optional. Fourth and fifth stands for start position and count.
Both are used in together. Sixth is compare mode, default is 0 (binary comparison) which means case
sensitive. If 1, then case in sensitive.
Space Function : It Returns a string consisting of the specified number of spaces.
Str1="Welcome"
Str2="to the QTP"
Str3="Learning Blog"
Str4="By Nilanjan"
MsgBox Str1 & Space(10) & Str2 & Space(2) &Str3 &Str4
'Output--> Welcome to the QTP Learning BlogBy Nilanjan
Split Function : it Returns an one-dimensional array containing a specified number of substrings.
'Comma As Delimitter
Str="Welcome,to,the,QTP, Learning,Blog"
'Split
5. SArray=Split(Str,",",-1)
For i= 0 to UBound(SArray)
Msgbox SArray(i)
Next
SArray here holds the returned array
StrComp Function : It Returns a value indicating the result of a string comparison.
Str="Welcome into the QTP Learning Blog"
Str1 = "QTP"
Str2 = "qtp"
Msgbox StrComp(Str1, Str2, 1)
'Output--> Returns 0 , which means Str1 is equal to Str2 for textual comparison
Msgbox StrComp(Str1, Str2, 0)
'Output--> Returns -1 , which means Str2 is greater than Str1 for Binary comparison
Msgbox StrComp("Nilanjana", "Nilanjan", 0)
'Output--> Returns 1
StrReverse Function : It Returns a string in which the character order of a specified string is reversed.
Str="Welcome to the QTP Learning Blog”
msgbox StrReverse(Str)
'Output-->gplB gninraeL PTQ eht ot emocleW
Msgbox StrReverse("Mindfire Solutions")
'Output-->snoituloS erifdniM
Trim Functions : t Returns a copy of a string without leading and trailing spaces
LTrim : It Returns a copy of a string without leading spaces
Rtrim: It Returns a copy of a string without trailing spaces
Example:
6. 'LTrim
Msgbox Ltrim(" Welcome to the QTP Learning Blog ")
'Output-->"Welcome to the QTP Learning Blog “
Example:
'RTrim
Msgbox Rtrim(" Welcome to the QTP Learning Blog ")
'Output--> " Welcome to the QTP Learning Blog "
Example:
'Trim
Msgbox Trim(" Welcome to the QTP Learning Blog ")
'Output-->"Welcome to the QTP Learning Blog "
InStr Function : It Returns the position of the first occurrence of one string within another string.
Str="Welcome to the QTP Learning Blog.qtp is a testing tool"
msgbox Instr(1, Str, "QTP", 1)
'Output--> 16 , which means it found the string in 16th position for textual comparison
msgbox Instr(6, Str, "qtp", 0)
'Output--> 34 , which means it found the string in 34th position for binary comparison.
Search started from 6th character.
msgbox Instr(6, Str, "Ftp", 0)
'Output--> 0 , which means it did not found the string after the 6th position
for binary comparison