CodeItBetter Programming Another VB Programming Blog

How to find the first number in a string

Posted on January 4, 2009
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
'String Manipulation - How to find the first number in a string
'This will return the First number found in the String (includes optional FirstPosition of string)
'This will also find decimal numbers.

Public Function FindFirstNum(ByVal MyString As String, Optional ByVal FirstPos As Integer = 1)
    Dim I As Integer, NumFoundFlag As Boolean, StartPos As Integer
    Dim Char As String
    For I = FirstPos To Len(MyString)
        Char = Mid$(MyString, I, 1)
        If IsNumeric(Char) = True And NumFoundFlag = False Then
            NumFoundFlag = True
            StartPos = I
        ElseIf (IsNumeric(Char) = False And Char <> ".") And NumFoundFlag = True Then
            GoTo ExitHere
        End If
    Next
 
ExitHere:
    FindFirstNum = Mid$(MyString, StartPos, I - StartPos)
End Function
 
'How can I call this function:
'Debug.Print FindFirstNum("asdfsdx8a.9a,4,59sdf04dfd80 ) d99 - dk98")
'will display 8

Related posts:

  1. How to find the first number with thousand separator in a string
  2. How to Find First Numbers of String (along with alphas & decimals)
  3. How to Check whether the given string has only numbers
  4. How to remove Leading/Trailing Spaces from the String
  5. How to check if a string contains only Numeric Characters
  6. How to Extract only numbers from a given string
  7. How to find a string between two substring on a given string phrase
  8. How to delete a string in another comma delimited string
  9. How to Find a String within another String with many options
  10. How to Remove two or more hyphens from a string

Comments (0) Trackbacks (0)

No comments yet.


Leave a comment


No trackbacks yet.