Service Functions - String (QTP, VBScript)
In the code examples I present I often refer to routine functions.
In my framework I have specialized libraries to call from. In my blog I maintain the similar structure.
Matching string with a pattern (Regular Expression, RegEx)
Public Function Regex_Test(ByVal strSrc, ByVal strRegEx) Dim objRegEx Dim boolRC, intRC Set objRegEx = New RegExp objRegEx.Pattern = strRegEx On Error Resume Next boolRC = objRegEx.Test(strSrc) intRC = Err.Number On Error GoTo 0 If intRC <> 0 Then boolRC = False Set objRegEx = Nothing Regex_Test = boolRC End Function
Public Function Regex_Match(ByVal sSource, ByVal sPattern, ByVal intIndex, ByVal boolMatchCase)
Dim objRegExp, objMatchColl
Dim boolRC, intRC
If intIndex < 0 Then
intIndex = 0
End If
Set objRegExp = new RegExp
objRegExp.Global = TRUE
objRegExp.IgnoreCase = Not boolMatchCase
objRegExp.Pattern = sPattern
On Error Resume Next
boolRC = objRegExp.Test(sSource)
intRC = Err.Number
On Error GoTo 0
If intRC <> 0 Then boolRC = FALSE
If Not boolRC Then
Set objRegExp = Nothing
Regex_Match = ""
Exit Function
End If
Set objMatchColl = objRegExp.Execute(sSource)
If (intIndex+1) > objMatchColl.Count Then
Set objMatchColl = Nothing
Set objRegExp = Nothing
Regex_Match = ""
Exit Function
End If
Regex_Match = objMatchColl.Item(intIndex).Value
Set objMatchColl = Nothing
Set objRegExp = Nothing
End Function
Replace pattern(s) in a string
Public Function ReplaceEx(ByVal sSource, ByVal sPattern, ByVal sDest, ByVal boolMatchCase)
Dim objRegExp
If sPattern = "" Then
ReplaceEx = sSource
Exit Function
End If
Set objRegExp = new RegExp
objRegExp.Global = TRUE
objRegExp.IgnoreCase = Not boolMatchCase
objRegExp.Pattern = sPattern
ReplaceEx = objRegExp.Replace(sSource, sDest)
Set objRegExp = Nothing
End Function
Number conversion and initialization with a default value
Public Function IntVal(ByVal sVal)
If isNumeric(sVal) Then
IntVal = CLng(sVal)
Else
IntVal = 0
End If
End Function
Public Function InitLong(ByVal sActualValue, ByVal DefaultValue)
DefaultValue = IntVal(DefaultValue)
If sActualValue = "" Then
InitLong = DefaultValue
Else
If isNumeric(sActualValue) Then
InitLong = CLng(sActualValue)
Else
InitLong = DefaultValue
End If
End If
End Function
Initialize string with a default value
Public Function InitValue(ByVal sActualValue, ByVal DefaultValue) If sActualValue = "" Then InitValue = DefaultValue Else InitValue = sActualValue End If End Function
Initialize boolean with a default value
Public Function InitBool(ByVal sActualValue, ByVal boolDefaultValue)
sActualValue = UCase(sActualValue)
Select Case boolDefaultValue
Case TRUE
'By default set to TRUE
'Set to FALSE if explicitely defined
If (sActualValue = "FALSE") OR (sActualValue = "NO") Then
InitBool = FALSE
Else
InitBool = TRUE
End If
Case FALSE
'By default set to FALSE
'Set to TRUE if explicitely defined
If (sActualValue = "TRUE") OR (sActualValue = "YES") Then
InitBool = TRUE
Else
InitBool = FALSE
End If
End Select
End Function
Questions / Answers
How to trim words inside a text line (QTP, VBScript)
How to wrap text line (QTP, VBScript)
Inserting breaks into text line


2 responses to "Service Functions - String (QTP, VBScript)"
Hi Albert!
Can you email me a QTP function that wraps line of text without breaking words?
Thank you!
[ Albert’s reply.
I actually didn’t have that function in my QTP libraries. Enjoyed working on this little exercise :)
Here we go: How to wrap text line ]
hello,
What QTP function i can use to trim inside string? For e.g. “text text text text” - i want only 1 space between.
Thanks!
[ Albert’s reply.
Raj, there is no such function but you can easily implement that in a variety of ways. Choose best for your context.
Sample solutions are here: How to trim words inside a text line ]