01.VB Script - Part I
01.VB Script - Part I
VBScript is an interpreted script language from Microsoft that is a subset of its Visual Basic
programming language designed for interpretation by Web browsers.
String Functions
⇒Len Function
Len : Returns the number of characters in a string or the number of bytes required to store a
variable.
Arguments:
Example:
Str="Welcome to the World of QTP"
Print Len(Str)
' Output --> 27
⇒LCase Function
LCase: Returns a string that has been converted to lowercase.
Syntax: LCase(string)
Aruguments:
Example:
Str="Welcome to the World of QTP"
Print LCase(Str)
'Output --> welcome to the world of qtp
Print Lcase("Good Morning")
'Output --> good morning
⇒UCase Function
Ucase: Returns a string that has been converted to uppercase.
Syntax: UCase(string)
Arguments:
Example:
Str="Welcome to the World of QTP"
Print Ucase(Str)
'Output --> WELCOME TO THE WORLD OF QTP
⇒Left Function
Left: Returns a specified number of characters from the left side of a string.
Arguments:
String expression from which the leftmost characters are returned. If string contains Null, Null is
String:
returned.
Numeric expression indicating how many characters to return. If 0, a zero-length string("") is
length: returned. If greater than
or equal to the number of characters in string, the entire string is returned.
Example:
Str="Welcome to the World of QTP"
print Left(Str,3)
'Output --> Wel
⇒Right Function
Right: Returns a specified number of characters from the right side of a string.
Syntax: Right(string, length)
Arguments:
String expression from which the rightmost characters are returned. If string contains Null, Null is
String:
returned.
Numeric expression indicating how many characters to return. If 0, a zero-length string is returned. If
length:
greater than or equal to the number of characters in string, the entire string is returned.
Example:
Str="Welcome to the World of QTP"
print Right(Str,12)
'Output --> World of QTP
⇒Mid Function
Aruguments:
String: String expression from which characters are returned. If string contains Null, Null is returned.
Character position in string at which the part to be taken begins. If start is greater than the number of
Start:
characters in string, Mid returns a zero-length string ("").
Number of characters to return. If omitted or if there are fewer than length characters in the text
length: (including the character
at start), all characters from the start position to the end of the string are returned.
Example:
Str="Welcome to the World of QTP"
print Mid(Str,9,12)
'Output --> to the World
⇒Replace Function
Replace: Returns a string in which a specified substring has been replaced with another substring
a specified number of times.
Arguments:
(Optional) Numeric value indicating the kind of comparison to use when evaluating substrings.
See Settings section for values. If omitted, the default value is 0, which means perform a binary
compare:
comparison.
Example:
Str="Welcome to the World of QTP"
⇒Space Function
Syntax: Space(number)
Arguments:
number: number of spaces you want in the string.
Example:
Str="Welcome to the World of QTP"
'Space
Str1="Welcome"
Str2="to the World"
Str3="of QTP"
⇒Split Function
Arguments:
Example:
'Space as Delimiter
Str="Welcome to the World of QTP"
'Split
SArray=Split(Str," ",-1)
For i= 0 to UBound(SArray)
Print SArray(i)
Next
'Comma As Delimitter
Str="Welcome,to,the,World,of,QTP"
'Split
SArray=Split(Str,",",-1)
For i= 0 to UBound(SArray)
Print SArray(i)
Next
⇒StrComp Function
Arguments:
Example:
Str="Welcome to the World of QTP"
StrComp
Str1 = "QTP"
Str2 = "qtp"
⇒StrReverse Function
StrReverse: Returns a string in which the character order of a specified string is reversed.
Syntax: StrReverse(string1)
Arguments:
Example:
Str="Welcome to the World of QTP"
StrReverse
print StrReverse(Str)
'Output-->PTQ fo dlroW eht ot emocleW
LTrim; RTrim; and Trim: Returns 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)
Arguments:
The string argument is any valid string expression. If string contains Null,
string:
Null is returned.
Example:
'LTrim
'Output-->"Welcome to QTPWorld.com
Example:
'RTrim
Arguments:
(Optional) Numeric expression that sets the starting position for each search. If omitted, search
begins at the first character position. If start contains Null, an error occurs. The start argument
start:
is required if compare is specified.
Example:
Str="How do you DO ?"
'InStr
⇒InStrRev Function
InStrRev: Returns the position of an occurrence of one string within another, from the end of
string.
Arguments:
(Optional) Numeric value indicating the kind of comparison to use when evaluating
compare: substrings. If omitted, a binary comparison is performed.
Example:
Str="How do you DO ?"
'InStrRev
Print InStrRev(Str,"DO",-1,1)
'Output--> 12 , which means it found the string in 12th position for
textual comparison
Print InStrRev(Str,"do",-1,0)
'Output--> 5 , which means it found the string in 5th position for binary
comparison
Print InStrRev(Str,"DO",13,0)
'Output--> 12 , which means it found the string in 12th position for binary
comparison
Print InStrRev(Str,"DO",10,1)
'Output--> 5 , which means it found the string in 5th position for textual
comparison