Blog to understand automation concepts in QTP, Selenium Webdriver and Manual Testing concepts

VB Script functions to manipulate strings in QTP

We should know the useful vb script function to manipulate strings. In this post, we will discuss some of the vb script function to manipulate strings and how they can be used in codes:

1. Instr searches for a string within another another string and returns the location of occurrence of one string within another string.It returns the position of first occurrence of the string.This function is used if we need to verify if a particular text appears in a string.


Syntax : InStr([start,]string1,string2[,compare])


Example - Report and loop in case a particular text in not found in the string :

If (Instr(1, string1, string2)>0) Then
    Reporter.ReportEvent micpass , "string 2 is found in string 1","As expected"
Else
    Reporter.ReportEvent micfail , "string 2 is  found in string 1","string 2 is not found in string1"
End If

2. InstrRev is similar to instr and can be used for similar purposes, with only difference search starts from the end of string, although count returned is from start of string.


InStrRev(string1,string2[,start[,compare]])


3. Trim , lTrim, Rtrim removes characters from both left and right, only left and only right respectively blank spaces.


4. Left , Right, and Mid returns a specified character from left, right, and middle of the string. 


Mid(string,start[,length])

5. len returns number of characters in the string.


6. Split splits a string based on the delimiter in the function.


Example - How to find the total number of occurrence of a string in another string.By default, string2 has value space if not provided.

arrString = split(string1,string2)
totOccurence = ubound(arrString)

Example- Total number of words in a string.

arrString = split(string1)
totOccurence = ubound(arrString)




No comments:

Post a Comment