This section discusses functions used for string manipulation.
Lease and Ucase
These two functions convert a string from lower top upper esse or vice versa:
'Convert to lower case
x = LCase("aBc") ' this will assign "abc" to x
'Convert to upper case
x = Ucase("aBc") ' this will assign "ABC" to x
This function compares two strings and returns 0 when they are equal:
x = StrComp("abc","abc") ' this call returns 0
x = StrComp("abc","aBc") ' this call returns non-zero
'The following call returns 0, as an explicit case insensitive
comparison is performed x = StrComp("abc", "aBc",vbTextCompare)
This function reversesa string's characters:
'this call returns "yhW" x = StrReverse("Why")
This function returns the length of a string:
'this call returns 3 x = Len("abc")
This function returns the specified number of characters from the left side of a string:
'this call returns "This is" x = Left('This is a test" ,7)
This function returns the specified number of characters from the right side of a string:
'this call returns "test"
x = RightO'This is a test",4)
This function returns the specified number of characters from the middle of the string. If the number of characters is not specified then it will return the string from the specified mid to the end
' this call returns "is a test" x = MidCThis is as test",6)
'this call returns "is", as we specified 2 characters
'starting at the 6th character position
x = MidCThis is as test",6, 2)
This function returns the starting character position of a sub-string in another string. It returns 0 when the substring is not found, as character positions within a string starts from 1:
'this call returns 6
x = InStr ("Find abcin this","abc")
'this call returns 0
x = InStr ("Find abc in this","aBc")
'this call returns 6
x = InStr ("Find abc in this","aBc", vbTextCompare)
'We can also specify a character position to start the search
'Returns 6, This is equivalent to default behavior, as we are
'saying to start the search from the 1st position in the string x = InStr (1, "Find aBc in this","abe")
'This call returns 0 as the search begins at the
'7th character, meaning the search string to be
'considered is "be in this" x = InStr (7, "Find abc in this","abc")
Problem . How can we find the current page and the total page values from the string "Page 4 of 15"?
First, we search for the " of " phrase within the full string (with a space character at the start and the end).
s_Text = "Page 4 of 15"
'Text to search for s_Search =" of "
'returns 7, the start of " of " i_Index = InStr (s_Text, s_Search)
Next, the length of our search string, " of", is 4 characters.
So to get the total pages value we need to read all characters after 4 + ilndex. We have two ways to extract this portion of the string either use the Right function or use the Mid function. Both approaches are shown below, but the Mid function will produce a cleaner code:
'Get text right from the search string
s_AllPages = Right (s_Text, Len(s_Text) - i_Index - Len(s_Search))
'We need to know the # of characters that we need to
'take from the Right of the string and Lent's Text) will
'give the length of the string " of 15"
' and we need to subtract the length of " of "
'also to get the 3 of characters in the all pages count
s_AHPages = Mid (s_Text, i_Index + Len (s_Search))
Now the current page count always appears from the 6th position in the string and goes till the start of the " of" string:
'We took the string from the left of " of " and then took the 'string from the 6th position to get the current page no.
s_CurrentPage = Mid(Left(s_Text, i_Index -1), 6)
roblem . How can we count the number of "if" phrases in the "if + if = 2 if" string?
s_Text = "if + if a 2 if"
'text to search for
s_Search = "if"
' this variable is used to store the last index found
'and then search the string again from 1 character forward
'to the next found string position
i_IndexFound = 0
'this variable will store the 8 of "if" strings found
i_IndexFound = InStr(i_IndexFound + 1, s_Text, s_Search)
'If found increase the counter If i_IndexFound> 0 then
i_NumCount = i_NumCount + 1 Else
Exit Do End If ' An infinite loop ended by a condition inside the loop Loop while (True)
'This will give a value 3 MsgBoxi_NumCount'
There is another way of doing this:
s_Text = "if + if * 2 if"
'Search string s_Search = "if"
'Delete the "if" phrases from s_Textandrfrote the difference in lengths i_NumCount = (Len(s_Text) - Len(Replace(s_Text,s_Search,""))) / Len(s_Search)
' Will give 3 MsgBoxi_NumCount
This function is also used to search for a sub-string in a string but searches from the end of the string. It does not reverse the string and then search it.
' this call returns 0
x = InStrRev ("abc","cba")
' this call returns 5
x = InStrRev ("abcabc","abc")
Below is an example which should make it clear when it is better to use InStr and when it is better to use InStrRev
Problem.Givenacompletefilepathspecification,extractthefilename,filepath and drive letter from the specification
The complete file path specification is "C:YfestStringsFunction.vbs' We know that the file name must appear after the last "" in the string and the drive letter must appear before the first ""
s_File = "C: TestStringsFunction.vbs"
'Locate first slash i_FirstSlash = InStr(s_File,"V")
'Locate last slash
i_LastSlash = InStrRev(s_File,"")
Now we know the position of the first and the last slash:
'-1 in case we don't want the "i" in the resulting drive letter s_DriveLetter = Left (s_File, i_FirstSlash -1)
'+1 as we want to ignore the "" character and take
'the string from its right
s_FileName = Mid (s_File, i_LastSlash + 1)
'-1 as we want to ignore the "" character 'and take the string from its left s_FilePath = Left (s_File, i_LastSlash - 1)
LTrim, RTrim and Trim
These function removed white spaces from the left, right or both sides of the string:
' returns "test "
x = LTrim (" test ")
' returns "test"
x =RTrim (" test ")
' returns "test"
x = Trim (** test ")
This function is used to replace a substring with a string:
'returns "The value of 2 is 2" to x
x = Replace (" The value of 1 is 2", "1", "2")
This function is also useful in cases of parameterization. Suppose we input a XML to a system and want to parameterize some value in the XML, In that situation, use Replace function as follows:
'XML template string
'Replace the parameter KNAMER with actual value S-XML = Replace (s_XML,"%NAiE%n, "Tarun")
This function generates specified number of space characters.
' This call returns a string containing 2 space characters
x = Space (2)
This function generates a string of specified length for the given character.
' This call returns a string containing 3 'A' characters x = String (3, "ft")
This function returns the array containing a variable number of strings:
' returns an array containing 2 strings x = Array("Tarun","Lalwani")
This function joins the contents of an array into a string. Each array element placed in the string is separated by an optionally defined delimiter character if none is specified a space character is used:
'Get an array
x = Array("Tarun","Lalwani","wrote","this","book")
' returns "TarunLalwani wrote this book"
's_Text = Doin (x)
' returns "Tarun„Lalwani_Wrote_this_book"
s.Text = Doin (x, "_.")
This function performs the opposite actions of the Join function, thus creating an array of strings from an input string:
'Text to be split
s_Text = "Tarun_Lalwani_Wrote_this_book,"
'This will assign Array("Tarun","Lalwani","wrote","this","book")
x = Split (s_Text, ""_")
Problem . Let's revisit the page number extraction problem again. How can we find the current page and the total page values from the string "Page 4 of 15*7
In this approach we use the Split function to place each non-space sub-string into an individual array element. Thereafter we can easily extract the 1st and 3rd array elements to extract the current page number and the total number of pages respectively.
'String page numbers X = "Page 4 of 15"
'Split the string base on space as delimiter. 'This will return Array("Page","4","of "15") pages = Split(x," ")
' returns a "A" string t MsgBox pages(l)
' returns a "15" string MsgBox pages(3)
The VBScript Split functionof VBScript allows one delimiter character to be specified. In case we want to split a string into words we need multiple delimiters. Next we create an extended version of Split to achieve this task:
'pText - The text to be splited
'Delimiter - This is a set of characters which will be considered as
'each character in the delimiter string will be taken as a delimiter and not
'IncludeDelimiter - If set to True the resulting array will contain delimiters
'revDelimiter - If this is set to True then any character other than specified
'delimiter are considered as delimiters
Public Function SplitEx(ByValpText, ByVal Delimiter, ByValIncludeDelimiter,
'If a blank delimiter then split the whole string into array
'of characters ,.
If Delimiter = "" Then iCount = Len(pText) ReDimoutarray(iCount)
For i = 1 ToiCount
sChar = Mid(pText, i, 1)
outarray(i - 1) = sCharNext Else
'Variable to store the word sword = ""
iCount = Len(pText) ReDimoutarray(0)
'Loop through all characters in the string For i = 1 To iCount 'Get the current character sChar = Mid(pText, i, 1)"If the character is one of the delimiters 'and revDelimiter flag is false then 'create the word and store it in the result array If (InStr(Delimiter, sChar) <> 0 And Not revDelimiter) Then ' store the word, in the array If sword <> "" Then:
outarray(UBound(outarray)) = sword sword = ""
'Increase the array size by 1 ReDim Preserve outarray(UBound(outarray) + 1) End If
'If IncludeDelimiter flag is true then add
'the delimiter as well to the array If IncludeDelimiter Then
outarray(UBound(outarray)) = sChar
ReDim Preserve outarray(UBound(outarray) + 1) End If Elself (InStr(Delimiter, sChar) = 0 And revDelimiter) Then 'If the character is not in the delimiter string "and revDelimiter is TRUE then create the word If
sword <> "" Then
outarray(UBound(outarray)) = sword
sword = "s"
ReDim Preserve outarray(UBound(outarray) + 1)
End If ,
'The delimiter word is always added to the array
outarray(UBound(outarray)) = sChar
ReDim Preserve outarray(UBound(outarray) + 1) Else
'Continue to capture the-wG^rd
sword = sword + sCharEnd If Next
'In case word in not blank then add to the array
If sword <> "" Then
outarray(UBound(outarray)) = sword
ReDim Preserve outarray(UBound(outarray) + l)
End If End If
'Decrease the array size by 1 as we started with a array
'of length 1 when we had not data at all ReDim Preserve outarray(UBound(outarray) - 1)
'return the array SplitEx = outarrayEnd Function
Now to split a statement into words or line we can use the SplitEx function in following ways:
'Returns Array("You", "can't", "do","this","can","you")
x = SplitEx("You can't do this, can you?", " ,?;.".False,False)
'Returns ArrayC'You", " ", "can't", " "."do"." "."this", "," . " ", "can", " ",
x = SplitEx("¥ou can't do this, can you?*", " ,?j.".True,False)
'Returns Array("Linel",MLine2", "Line3")
x = SplitEx("Linel" &vbCrLf& "Tinea" &vbCrLf& "Lines'", vbCrLf, False,False)
This function returns a sub-set array of the search array containing the specified substring:
'Get an array
X = Array ("Tarun'"LalWani"", "wrote", "this", "book")
'This will assign Array("Tarun", "wrote")
'to x as both of the elements contains "r" in that x_Found = Filter (x, "r")
This function converts all special characters to their escape values. For example a " " is converted into "%20" where 20 is ASCII code of " ". This is used in HTML to encode special character and pass it as a part of the URL. UnEscape method can be used to decode these string and convert the "%XXX" characters back to the actual character:
'encode a string
'x=,,http%3A//i!ycoBpany.coii/cart%3Factien%3DAdd%28Pxodld%3DX2%26QTY%3D2" x = EscapeO'http://mycompany. com/cart?action=AddfrProdId=128'QTY=2")
'decode a encoded string
Using Qtp Help
Qtp Environment Variables
Descriptive Programming (dp)
Debugging In Qtp
Working With Apis
Qtp Automation Object Model
Working With Xml
Working With Databases
Working With Microsoft Excel
Working With Microsoft Outlook
Working With Microsoft Word
Working With Microsoft Internet Explorer
Working With Hp Quality Center
Working With .net Classes
All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd
Wisdomjobs.com is one of the best job search sites in India.