Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Simple, getting an integer out of a string?

Status
Not open for further replies.

99mel

Programmer
Oct 18, 1999
379
GB
Yello,

Whats the function to simply get an integer out of a string datatype!

strval = abcd19

i want the 19.

i know its somert simple!

Cheers
 
Set a reference to MS VBScript dll, then add this code:

Code:
Dim re as New RegExp, _
    eMatch As Match, _
    eMatches As MatchCollection

re.Global = False  ' first match only, true is all matches
re.Pattern = "\d*" ' Any number of digits [0-9] in a row

eMatches = re.Execute("abcd19")  ' Creates a collection of
                                 ' all the matches

For Each eMatch in eMatches      ' Loop through all matches
    Msgbox eMatch.Value
Next

Hope it helps!

Kevin
 
Or you could do this ( no references required !!)

x = "xxxx12"

For lnLoop = 1 To Len(x)
y = Val(Right$(x, Len(x) - lnLoop))
If y > 0 Then Exit For
Next

MsgBox y

Dan.
 
Try this. This will pick up all the digits at the end of the string
Code:
Dim strVal  As String, _
    intNum  As Integer, _
    i       As Integer
    
strVal = "abcd19"

For i = 1 To Len(strVal)
   If IsNumeric(Mid(strVal, Len(strVal) - (i - 1), 1)) Then
      intNum = Val(Right(strVal, i))
   Else
      Exit For
   End If
Next i

MsgBox intNum
Hope this helps,
Tim
 
Actually, the question and examples are to simplistic.

Is the overall string ALWAYS of the same format (e.g. six total chars with the LAST two being the numeric part)?

Is there always one "value" in the string (or could there be more - or even LESS)?

Is the Value(s) always an Integer?

How should (?unexpected?) variations be treated?

What should be returned in cases where the routine does not resolve the input into the (?desired?) result?

MichaelRed
mred@att.net

There is never time to do it right but there is always time to do it over
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top