Monday, 20 May 2013

Working with strings related functions in QTP with the use of VBScript.

Here I will be covering commonly used string related functions with examples.


 1.     Len : Returns the number of characters in a string
 ‘Usage
 MyStr = “Working with Strings”
 MyStrLen=Len(MyStr)
 msgbox MyStrLen
 Result: QTP shows number of characters in the string in a dialog box

 2.     LTrim : Retruns the copy of a string by removing  leading spaces
 ‘Usage
 MyStr = “                  Working with Strings               ”
 MyStr1 = LTrim(MyStr)
 msgbox MyStr1                 ‘Remove the leading spaces in the string
 Result:  QTP shows the string by removing the leading spaces of the string in a dialog box

 3.     RTrim : Retruns the copy of a string by removing  trailing spaces
 ‘Usage
 MyStr = “                  Working with Strings               ”
 MyStr2 = RTrim(MyStr)
 msgbox MyStr2                 ‘Remove the trailing spaces in the string
 Result:  QTP shows the string by removing the trailing spaces of the string in a dialog box

 4.     Trim : Retruns the copy of a string by removing  both leading and trailing spaces
 ‘Usage
 MyStr = “                  Working with Strings               ”
 MyStr3 = Trim(MyStr)
 msgbox MyStr3                  ‘Remove both the leading and trailing spaces in the string
 Result: QTP shows the string by removing both the leading and trailing spaces of the       string in a dialog box

 5.     Left : Returns the specified number of characters of the string from left side
 ‘Usage
 MyStr = “Working with Strings”
 MyStrLeft = Left(MyStr,5)
 msgbox MyStrLeft              ‘Returns the specified number of characters(5) in the  string from left side
 Result: QTP shows the specified number of characters of the string from left side in a  dialog box

 6.     Right : Returns the specified number of characters of the string from right side
 ‘Usage
 MyStr = “Working with Strings”
 MyStrRight = Right(MyStr,4)
 msgbox MyStrRight            ‘Returns the specified number of characters(4) in the  string from Right side
 Result: QTP shows the specified number of characters of the string from right side in a  dialog box

 7.     Mid : Returns the specified number of characters from a string
 ‘Usage1
 MyStr = “Working with Strings”
 MyStrMid = Mid (MyStr,5)
 msgbox MyStrMid              ‘Returns all the characters starting from 5th position in the  string
 Result: QTP shows all the characters starting from 5th position in the string

 ‘Usage2
 MyStr = “Working with Strings”
 MyStrMid1 = Mid (MyStr,5,3)
 msgbox MyStrMid1             ‘Returns 3 characters starting from 5th position in the  string
 Result: QTP shows 3 characters starting from 5th position in the string

 8.     UCase : Converts the specified string to Uppercase
 ‘Usage
 MyStr = “Working with Strings”
 MyUpper = UCase(MyStr)
 msgbox MyUpper               ‘Returns all the characters in the string to uppercase
 Result: QTP shows all the characters in the uppercase of the specified string in a dialog box

 9.     LCase : Converts the specified string to Lowercase
 ‘Usage
 MyStr = “Working with Strings”
 MyLower = LCase(MyStr)
 msgbox MyLower               ‘Returns all the characters in the string to Lowercase
 Result: QTP shows all the characters in the lowercase of the specified string in a dialog box

 10.  InStr : Returns the position of the first occurrence of one string with in another  string. This is used for search a string within the string. The search begins at the first  character of the string.
 ‘Usage
 MyStr = “Order number 12345 created successfully”
 MySubStr = “12345”
 Pos=InStr (MyStr,MySubStr)
 msgbox Pos             ‘Returns the position of the first occurrence of the sub string in  the string . If position is 0 then sub string is not available in the string.
 Result: QTP shows position of the first occurrence of the sub string in the string in a dialog box

 11.  StrReverse : Returns the reversed string
 ‘Usage
 MyStr = “Hello World”
 MyReverse = StrReverse (MyStr)
 msgbox MyReverse            ‘Returns the reversed string
 Result: QTP shows reversed string in a dialog box

 12.  StrComp : Compares two strings and returns a value
 a.    Both strings are equal, returns 0
 b.    String1 is greater than String2, returns 1
 c.    String2 is greater than String1, returns -1

 ‘Usage
 MyStr1 = “Hello World”
 MyStr2 = “Hello”
 MyVal = StrComp (MyStr,MyStr1)
 msgbox MyVal                   ‘Returns a value that represents the result of the  comparision
 Result: QTP shows the value in a dialog box


 13.  Here I wanted to explain a scenario with the script by using date and string functions  . For example your application allows the format dd-mm-yy format only. How to handle  this? Below is the solution.
 ‘Usage
 Sysdate = Now
 Sysyear = Year(Sysdate)               ‘Year is in yyyy format so need of any changes
 ‘As per requirement I wanted to use only 2 digits of current year
 ‘Convert the year to string
  yyyy=Cstr(Sysyear)
 ‘Need to get only two digits in the right side
 yy=Right (yyyy,2)
 Sysmonth=Month(Sysdate)           ‘ Here month can be one or two digits
 Mm=Len(Sysmonth)                    ‘ getting the length of the string
 If mm < 2 then
 Retmonth=”0”&mm                    ‘ If the month is one digit then we can concatenate 0  to month
 Else
 Retmonth=mm
 End if
 Sysday=Day(Sysdate)                  ‘ Here day can be one or two digits
 dd=Len(Sysday)                ‘ getting the length of the string
 If dd < 2 then
 Retday=”0”&dd                ‘ If the day is one digit then we can concatenate 0 to day
 Else
 Retday=dd
 End if
 ‘Now concatenate the strings to required format dd-mm-yy
 reqFormat = Retday&”-“&Retmonth&”-“&yy
 msgbox reqFormat

No comments:

Post a Comment

Search This Blog

There was an error in this gadget