In test automation scripting you have to deal a lot with strings and numbers and should know how to extract or retrieve one from the other. Sometime back, there was an interesting question posted on a forum. I am not posting the exact question here but its variation for more clarity.

Given a dynamic string, for example 654ABC or 5432FGHJS or 9GFHSK , how will you extract all the numbers that appear in the front part of the string?

Your code should be adaptable enough to take any string and output the numerical digits appearing in-front part of the string.

The answers we are looking from above strings are 654, 5432 and 9.

Please post your code in the comments section below.


We are really overwhelmed by the response of UFT community towards the quiz. There were some excellent answers there.
There is no right or wrong solution here. At times we may know all the methods to solve a problem but we tend to use the one which strikes us first.

Checking how others would approach and solve the same problem is the best way to sharpen your scripting skills.

While there can be multiple ways to arrive at the solution, my preferred way in this case would be to use RegExp object to find the digits quickly. Using For-Loop is a performance intensive method.

Dim s : s = "654ABC"

Set re = New RegExp 'Create Regular expression object
re.Pattern = "^\d+" 'Matches if a string starts with a digit

Set matches = re.Execute(s) 'Collection

If matches.Count <> 0 Then
msgbox matches(0).Value 'Output the first value if it exists
msgbox "The input string " & s & " doesn't start with a digit"
End If

If you want to keep track of further articles on UFT (QTP). I recommend you to subscribe by Email and have new UFT articles sent directly to your inbox.

Subscribe to get free updates on UFT!

LearnQTP is the most popular site on UFT (formerly QTP).

Enter your first name and email address below to instantly subscribe and download an eBook on Optimizing UFT Scripts. In future, we will make sure you get new tips & tricks on UFT delivered direct to your email box.

Please check your email and confirm free subscription!