Wednesday, September 12, 2012

String Functions in QTP

'string functions examples
' LTRIM() Left space remove function
str1="           Remove left space"
msgbox Ltrim(str1)

'RTRIM() Right space Remve function
str2="Remove right space              "
msgbox Rtrim(str2)

'Trim() Remove left and right space
str3= "       Remove left and right space           "
msgbox Trim(str3)

'LEN() to find the length of string function
str4= "find the length of string"
msgbox "Length of string is " &Len(str4)

'LCASE() UCASE() upper and lower case letter function
str5="AutoMATion word in Lower and UPPER case"
msgbox "in lowercase later"  &LCASE(str5)
msgbox "In uppercase later" & UCASE(str5)

'InStr function helps you determine if a smaller string is present in a bigger string or not.
'If a match is found, this functions returns a number that represents the position at which the match is found
sBigString = "Automation Repository"
sSmallString = "on Rep"
'Start match from 11th character
iReturnVal = InStr(11, sBigString, sSmallString, 1)
'From the above statement iReturnVal will have value 0 because we are matching from 11th character.
'So effectively, we are trying to find "on Rep" in "Repository"
msgbox "result of instr function" &iReturnVal

'InstrRev() function
iReturnValRev=InStrRev(sBigString, sSmallString)
msgbox "result of InStrRev function" &iReturnValRev

'Left function to find out a specified number of characters from the left of a string. Syntax: Left(String, Length)
Dim sMainString, sLeftString
sMainString = "Orange"
sLeftString = Left(sMainString, 2) 'Get the first two characters from the left of the string
msgbox  "Displays the value Or" &sLeftString

'Right function retrieves the specified characters from the right side of the string. Syntax: Right(String, Length)
sRightString = Right(sMainString, 2)
msgbox  "Display the value ge" &sRightString

'splits a string using a delimiter and returns a number of sub-strings stored in an array
'Syntax: Split(String, Delimiter(optional), Count(optional), Compare(optional))
sText = "Yes,No,Maybe"
sDelimiter = "," 'Delimiter is comma (,)
'Split the text using the delimiter. 'arrArray' is the array in which the fucntion Split will store the sub-strings
arrArray = Split(sText, sDelimiter)
'arrArray(0) contains the value Yes. arrArray(1) contains the value No and  arrArray(2 contains the value Maybe
'Display the sub-strings in a msgbox
iLoop = UBound(arrArray) 'Find trhe size of the array
For i = 0 to iLoop
   msgbox  "Split function result" &arrArray(i)
Next

'Mid function can be used to retrieve a sub-string from a main string Mid(String, Start, Length(optional))
Dim sMainString1, sSubString1, sSubString2
sMainString1 = "Orange"
sSubString1= Mid(sMainString1, 3, 2) ' variable 'sSubString' will have the value 'an'
msgbox "Substring upto length 2 by mid function" &sSubString1
sSubString2 = Mid(sMainString1, 3) ' variable 'sSubString1' will have the value 'ange'
msgbox "Sub string using mid function without length parameter" &sSubString2

'Replace function can be used to replace some sub-string within a main string with some other string.
replacestr="abaacaadea"
msgbox "Replace abaacaadea a character with b" &Replace(replacestr, a,b)

' Space function Returns a string consisting of the specified number of spaces
Dim MyString
MyString = Space(10)   ' Returns a string with 10 spaces.
MyString = "Hello" & Space(10) & "World" ' Insert 10 spaces between two strings.
msgbox "Display string with 10 space insert between two string" &MyString
'String function to create a repeating character string of a specified length. str = String(5, “a”) would return the string – “aaaaa”.
Dim MyString1
MyString1 = String(5, "*")   ' Returns "*****".
msgbox "string func value" &MyString1
MyString1 = String(5, 42)   ' Returns "*****".
msgbox "string func value" &MyString1
MyString1 = String(10, "ABC")   ' Returns "AAAAAAAAAA".
msgbox "string func value" &MyString1

'StrReverse function in QTP can be used to create a reverse of a string.
strrev=StrReverse("Trupti")
msgbox "String Reverse function" &strrev

'Join()- If you have a number of sub-strings in an array, you can use the Join function to concatenate all the sub-strings from the array into a single string.
Dim MyStringJoin
Dim MyArray3(3)
MyArray3(0) = "Mr."
MyArray3(1) = "John "
MyArray3(2) = "Doe "
MyArray3(3) = "III"
MyStringJoin = Join(MyArray3) ' MyString contains "Mr. John Doe III".
msgbox "Join String example" &MyStringJoin

'StrComp function can be used to compare two strings in QTP o means binary comparision and 1 means text comparision
Dim MyStr1, MyStr2, MyComp
MyStr1 = "ABCD": MyStr2 = "abcd"   ' Define variables.
MyComp = StrComp(MyStr1, MyStr2, 1)   ' Returns 0.
msgbox " text comparision ABCD abcd" &MyComp
MyComp = StrComp(MyStr1, MyStr2, 0)   ' Returns -1.
msgbox "binary compariion ABCD abcd" &myComp
MyComp = StrComp(MyStr2, MyStr1)   ' Returns 1.
msgbox "string comparision ABCD abcd" &MyComp

'Filter function returns a zero-based array containing a subset of a string array based on a specified filter criteria.
'If no matches of Value are found within InputStrings, Filter returns an empty array. An error occurs if InputStrings is Null or is not a one-dimensional array.
'The array returned by the Filter function contains only enough elements to contain the number of matched items
Dim MyIndex
Dim MyArray (3)
MyArray(0) = "Sunday"
MyArray(1) = "Monday"
MyArray(2) = "Tuesday"
MyIndex = Filter(MyArray, "Mon") ' MyIndex(0) contains "Monday".
msgbox "filter function output" &MyIndex(0)

No comments:

Post a Comment