Wednesday 19 October 2011

QTP Script: Using the replace method to find and replace text in a string


MsgBox ReplaceText("Automating with QTP is rubbish.""rubbish.""great!")

MsgBox ReplaceText("QTP is a great automation tool but I can't use it","but.*","!")

' =============================================================
' function: ReplaceText
' desc :    Uses a regular expression to replace text within a string
' params :  strString is the string to perform the replacement on
'           strPattern is the regular expression
'           strReplacement is the replacement string
' returns : The finished string
' =============================================================
Function ReplaceText(strStringstrPatternstrReplacement)

Dim objRegEx

' create the regular expression
Set objRegEx = New RegExp 

' set the pattern 
objRegEx.Pattern = strPattern

' ignore the casing
objRegEx.IgnoreCase = True

' make the replacement
ReplaceText = objRegEx.Replace(strStringstrReplacement

' destroy the object
Set objRegEx = Nothing

End Function ' ReplaceText 


1 comments:

  1. thanks testingbirds. thanks for providing valuable codes.

    ReplyDelete