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.
Answer
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 else 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 below and have new UFT articles sent directly to your inbox.
Ankur Jain says
Thank you all for the excellent answers. It certainly gives a perspective on how different people think differently while arriving at the same answers.
We have updated our answer in the post above.
Raja says
Chinnikrishna Darapureddy says
Mohammed Jabbar says
Ram.S says
Tejhaswini says
joe Daniel Lawrence says
Venkateswara Rao Saladi says
Answer : 9
Michael Bowen says
Subh says
Hung Cao says
There are two ways to do it…. It is depend what is your requirement and expect the result.
1. Non digit is “/D” so you can use the replace function to replace “/D” to “”. The drawback on this is following 123XX2V would return 1232. XX12ER3 would return 123.
2. Write a function except the string for input and return the number. Use the for loop to check every character. ignore when you see the char. 123XX2V would return 123. XX12ER3 would return 12.
Phamo says
ishan says
Chandan says
Deepak says
srinivasu ch says
Manya says
Raghavendra Rao Unnam says
Jagan says
vikash says
Amit Kumar Singh says
Hari says
Ravi says
Rohit Shinde says
Hi Ankur,
Please see the below piece of code to find the dynamic legnth number form dynamic string.
Lokesh Kumar says
Raghavendra Rao Unnam says
Jyotikrushna Mohapatra says
Jyotikrushna Mohapatra says
Harry McKay says