
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






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.
str_input=Array("654ABC","5432FGHJS","9GFHSK","ABC10","ABCD","123ABC986") str_FailMsg = "No Numbers appears in the first part of the given string" For intLP=0 to ubound(str_input) strOutput="" For intInLp=1 to len(str_input(intLp)) If asc(mid(str_input(intLp),intInLp,1))>=48 and asc(mid(str_input(intLp),intInLp,1))<=57 Then If isnumeric(mid(str_input(intLp),intInLp,1)) Then strOutput = strOutput & mid(str_input(intLp),intInLp,1) End iF Else Exit For End If Next IF strOutput="" Then print str_FailMsg Else print strOutput End IF Next' ------------------------------------------------------------------------------- ' Function : To Extract numbers from a string at any position & not just starting ' Date: 9/30/2016 ' Author: Ram S ' Project : LearnQTP Quiz ' Revision: 1.0 ' ------------------------------------------------------------------------------- Option Explicit Dim sstring, objRegEx, valMatch, inString, oString Set objRegEx = CreateObject("VBScript.RegExp") objRegEx.Global = True objRegEx.Pattern = "[^A-Za-z]" sstring = "5432FGHJS" Set inString = objRegEx.Execute(sstring) If inString.Count > 0 Then For Each valMatch in inString oString = oString & valMatch.Value Next Wscript.echo oString End If ' --- Output = 5432'Function to fetch the numeric data from a dynamic string Function FunRetrieveNumericDataFromString(vString) vNumericData = "" vStringLen = Len(vString ) for i = 1 to vStringLen vNumeric = Mid(vString ,i,1) If IsNumeric(vNumeric ) then vNumericData = vNumericData&vNumeric End If FunRetrieveNumericDataFromString= vNumericData Next End Function 'Ex: vString = "654ABC" 'By calling the below function we can get the Numeric data from a dynamic string. vRetrieveNumericData = FunRetrieveNumericDataFromString(vString)Answer : 9
Option Explicit Sub FindNumberInString() 'Variable declaration Dim strValue As String 'Input String Dim strLen As Integer 'Holds length of input string Dim OutString As String 'Holds Output String Dim i As Integer 'Index variable strValue = Worksheets("Sheet1").Range("B1").Value 'Read value into variable strLen = Len(strValue) 'Find length of entire string OutString = "" 'Zero OutString For i = 0 To strLen - 1 'Progress through each item Dim strItem As String 'Holds individual values as they are removed strItem = Mid(strValue, i + 1, 1) 'removes item If IsNumeric(strItem) Then 'Test to see if it is a number OutString = OutString + strItem ' Concatenates to OutString End If Next i Worksheets("Sheet1").Range("B2").Value = OutString 'Write OutString End Substr1=InputBox("Enter the string") strLength=len(str1) For X = 1 To strLength If IsNumeric(Mid(str1, X, 1)) = True Then nNum = nNum & Mid(str1, X, 1) else exit for End If Next MsgBox nNumThere 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.
Public Function ExtractNumericFromString(text) Set regEx = New RegExp regEx.Pattern = ".*\d+.*" regEx.IgnoreCase = True regEx.Global = True If regEx.Test(text)Then regEx.Pattern = "[^0-9]" results = regEx.Replace(text,"") Else resuts = "" End If ExtractNumericFromString = results End Function Wscript.Echo "654ABC: " & ExtractNumericFromString("654ABC") Wscript.Echo "5432FGHJS: " & ExtractNumericFromString("5432FGHJS") Wscript.Echo "9GFHSK: " & ExtractNumericFromString("9GFHSK")Sub test() Str1 = "3test4test" strlen = Len(Str1) For i = 1 To strlen char1 = Mid(Str1, i, 1) If IsNumeric(char1) Then num_val = num_val & char1 End If Next MsgBox num_val End SubDim intval 'varibale declaration Dim intval1 dim inval2 intval ="654ABC" 'or 5432FGHJS or 9GFHSK for i= 1 to len(intval) inval2=mid(intval,i,1) if IsNumeric(inval2) then counter= counter+inval2 end if next msgbox counterStrValue = InputBox("Please Enter String") Set objRgExp = New RegExp objRgExp.Global = True objRgExp.Pattern = "[0-9]+" Set strRes = objRgExp.Execute(StrValue) For Each res in strRes msgbox res.value,0,"String Found" NextHi Ankur,
Please see the below piece of code to find the dynamic legnth number form dynamic string.
Dim arrTemp(10) intArrIndex = 0 strVal = "654ABC or 5432FGHJS or 9GFHSK" For intIndex = 1 To Len(strVal) chrVal = Mid(strVal,intIndex,1) If IsNumeric(chrVal) Then strTemp = strTemp & cstr(chrVal) If Not isNumeric(Mid(strVal,intIndex + 1,1)) Then arrTemp(intArrIndex) = strTemp strTemp = "" intArrIndex = intArrIndex + 1 End If End If Next For intIndex = 0 To intArrIndex - 1 msgbox arrTemp(intIndex) Nextst="654ABC" x=len(st) Temp="" For i=1 to x y=mid(st,i,i) If isnumeric(y)="True" Then Temp=Temp&y Else Exit for End If Next msgbox Tempst="12345hgjkgjkhjkh" x=len(st) For i=1 to x y=mid(st,i,1) If isnumeric(y)="True" Then Temp = Temp&y Else Exit for End If Next msgbox Temp