Micro Focus QTP (UFT) Forums

Full Version: Find number in a string
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi, I am looking a function that takes a number from a string.

For example I have:

"The number of players was 86 and..."

Is there any native function of vbScripting that does this?

Thanks
Hello,
Below is the example code to get the number from string.
Code:
l_String="The number of players was 86 and..."
l_num=""
    For i=1 to len(l_String)
            y=mid(l_String,i,1)
                If ( isnumeric(y)=True) Then
                          l_num= l_num & y
                End If
    Next
    msgbox l_num
Code:
strString="The number of players was 86 and 88 hkjh 880"

    splitString=Split(strString," ")
    intCount= UBound(splitString)
    intCount= Cint(intCount)
    arrayNum=" "
    For i = 0 to intCount
        If IsNumeric(splitString(i)) Then
            arrayNumbers= splitString(i)
            If arrayNumbers<>""  and arrayNum =" "  Then
                arrayNum= arrayNumbers            
            Else
                arrayNum= arrayNum &"_"& arrayNumbers            
            End If
            
        End If
    
    Next
    MsgBox arrayNum
Thanks. Looking the string, it has several numbers, but the number i need is next to the word "código" (code in English), it can be done with regular expressions? Or I have to add some lines to the code below.

I will think the solution, if it works i will post it.
Solved

Code:
l_String=Datatable("o_DescRechazo", dtLocalSheet)
l_num=""
    For j=1 to len(l_String)
       y=mid(l_String,j,6)
       If ( y="Código") Then
         For i=j to len(l_String)
           y=mid(l_String,i,1)
           If ( isnumeric(y)=True) Then
              l_num= l_num & y
            End If
          Next
          j=len(l_string)                        
       End If
     Next
  
    msgbox l_num

Thanks to all
I understand it is solved. Just curious to know, If your text string was so,

txtstrng = "this is codeigo 86 testing numeric"

I would simply do this,

Code:
reqnum = Split(Split(txtstrng,"codeigo")(1),"testing")(0)
msgbox reqnum

Why run unnecessary loops ?

Food for thought mate...
It is an interesting solution... the "código" word is always going to be before the number i need, but i don´t know what is next to that word.
Btw, i don´t completely understand what are you trying to do there, why (1)? why (0)?

It is an interesnting alternative, with better performance...

Although it doesn´t work "The subindex is out of range: '[number: 1]'"
Hi,
Let me explain the below in detailed.
Code:
txtstrng = "this is codeigo 86 testing numeric"

reqnum = Split(Split(txtstrng,"codeigo")(1),"testing")(0)
Let us look into the inner split first and then the outer split.
When you use split method using "codeigo", then it will breake into 2 strings.
Split(txtstrng,"codeigo")(1):
  String(0) = "this is codeigo"
  String(1)= "86 testing numeric"
So, Split(txtstrng,"codeigo")(1) = "86 testing numeric"
Now here again we are spliting the string with "testing"

  Split(Split(txtstrng,"codeigo")(1),"testing")(0):
  Split("86 testing numeric","testing")
When you use split method using "testing", then it will breake into 2 strings.
string(0)= "86 "
string(1)="testing numeric"

As we need the numeric value. We have to capture the first string i.e. string(0).


Please let me know if you still have any questions.
Hi there,

You can use this way to get any number(s) from a string

Code:
Public sub s_GetNumberFromString (strText, byref colNum)
    Set ObjRegExp = New RegExp
    ObjRegExp.Pattern = "\d+"
    ObjRegExp.Global = True
    Set colNum = ObjRegExp.Execute(strText)
End Sub

Dim colNum
s_GetNumberFromString strNormal, colNum
For each intNum in colNum
    MsgBox intNum
Next

Ngoc Vo
Code:
Dim regEx: Set regEx = New RegExp
regEx.Pattern = "(?:código )(\d+)"
MsgBox regEx.Execute("this is código 86 testing numeric").Item(0).SubMatches.Item(0)

(?: ) is a non-capturing group, so you are assured the digits will end up in the first SubMatch of the SubMatches collection. If you want to break the code down a little further:

Code:
Dim txtstrng: txtstrng = "this is código 86 testing numeric"

Dim regEx: Set regEx = New RegExp
regEx.Pattern = "(?:código )(\d+)"

Dim regExMatches: Set regExMatches = regEx.Execute(txtstrng)

For i = 0 To regExMatches.Count-1
    MsgBox "código match number " & i & ": " & regExMatches.Item(i).SubMatches.Item(0)
Next

This will iterate through each match if there are more than one.
Code:
Dim var
var = inputbox("Enter the string with any numeric value", "Alpha Numeric", "Baba205Fakruddin")

Function Get_No_FromString(var)

    len_var = len(var)

    bln=""
    For i=1 to len_var
    
        cha =mid(var,i,1)
    
        If isnumeric(cha) Then          '1 or 2 or 3 or 4 or 5 or 6 or 7 or 8 or  9)
            bln = bln&cha
            print bln
        End If
    
    Next
    
    print "The digits in string"&bln

    Get_No_FromString=bln
End Function


No_in_String = Get_No_FromString(var)
msgbox "The digits available in string"&No_in_String