Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Find number in a string
#1
Solved: 11 Years, 3 Months, 4 Weeks ago
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
Reply
#2
Solved: 11 Years, 3 Months, 4 Weeks ago
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
Reply
#3
Solved: 11 Years, 3 Months, 4 Weeks ago
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
Reply
#4
Solved: 11 Years, 3 Months, 4 Weeks ago
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
Reply
#5
Solved: 11 Years, 3 Months, 4 Weeks ago
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...
Basanth
Give a fish to a man and you feed him for a day..Teach a man how to fish and you feed him for life.
Reply
#6
Solved: 11 Years, 3 Months, 4 Weeks ago
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]'"
Reply
#7
Solved: 11 Years, 3 Months, 4 Weeks ago
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.
Thanks,
SUpputuri
Reply
#8
Solved: 11 Years, 3 Months, 4 Weeks ago
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
Reply
#9
Solved: 11 Years, 3 Months, 4 Weeks ago
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.
Reply
#10
Solved: 11 Years, 3 Months, 4 Weeks ago Cool 
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  VB Script:number of times a character appears in a string with position Jyobtech 1 12,072 08-07-2013, 01:03 PM
Last Post: anil2u
  Find capital letter in a string Arul 2 8,801 12-27-2011, 11:11 PM
Last Post: Arul
  How to have a variable that has a string comma string .. shareq1310 5 5,080 11-09-2011, 03:33 PM
Last Post: parminderdhiman84
  How to count a repeated number in number in particular range gollsrin 1 3,584 04-28-2011, 11:41 AM
Last Post: Saket
  Removing commas from a string and changing it to number indranilgoswamimcb 3 8,657 12-31-2010, 10:40 AM
Last Post: rajeshwar

Forum Jump:


Users browsing this thread: 4 Guest(s)