Header Ads

Getting Number from String in QTP

Most of the time it is seen we need to capture a string and we need to find out the numeric value from that string.
Here is a code that will help to get Numerics from the String.

Function extract_number(msg)
Dim re, matches, item, result
Set re = New RegExp
re.pattern = "[A-Za-z -.]*(\d+).*"
Set matches = re.Execute(msg)
If matches.Count > 0 Then
Set item = matches(0)
If item.SubMatches.Count > 0 Then
result = item.SubMatches(0)
result = -1
End If
result = -1
End If
extract_number = result
End Function

MsgBox extract_number("There are 40 boys")
MsgBox extract_number("Ticket is confirmed and the no is 56477")

This is another method..

mystring = "The order is confirmed and its no is 567"
myLength = Len(mystring)

For i = 1 To myLength

If Asc(Mid(mystring, i, 1)) <> 32 Then
If Asc(Mid(mystring, i, 1)) >= 48 And Asc(Mid(mystring, i, 1)) <= 57 Then
myNumber = myNumber & Mid(mystring, i, 1)
End If
msgbox("no numeric")
End If

Powered by Blogger.