How to get numeric value from a string

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • SEhtesham
    New Member
    • Aug 2011
    • 45

    How to get numeric value from a string

    Hi,
    I have a StatusStrip and is filled with Year in the format say "June2009 - May2010".
    I want to get the years from this string like 2009,2010 from the above string.
    Please suggest how can i do this.
    I have tried this but not able to further separate June2009.
    Please have a look
    Code:
     Dim strYearDet As String()
     strYear11, strYear2 As String
     strYearDet = StatusStrip1.Items(0).Text.Split("-")
     strYear11 = strYearDet(0)
     strYear2 = strYearDet(1)
    Can anyone help of how to get 2009 and 2010 from the string
  • Frinavale
    Recognized Expert Expert
    • Oct 2006
    • 9749

    #2
    Use the Regex Class to retrieve the numbers from the string.

    For example:
    Code:
    Dim theString As String = "June2009 - May2010"
    Dim numRegEx As New System.Text.RegularExpressions.Regex("[0-9]{4}")
    Dim matches As System.Text.RegularExpressions.MatchCollection = numRegEx.Matches(theString)
    Dim num1 As Integer
    Dim num2 As Integer
    If matches.Count = 2 Then
      Integer.TryParse(matches(0).Value, num1)
      Integer.TryParse(matches(1).Value, num2)
    End If
    -Frinny

    Comment

    Working...