Trim everything BUT numbers from a string.

Posted on 2006-04-07
Last Modified: 2010-04-23
what is the best way to trim all characters and special characters like ( ) from a string leaving JUST the numbers.

So the string would look like:

323 (NoNote)

So I would want to trim , letters, special characters and spaces.

Thanks in advance.
Question by:cjinsocal581
    LVL 10

    Assisted Solution

           Dim a, b As String
            a = "123 % Hello $ 456 world 789 $"

            '  b = "12345678"
            b = System.Text.RegularExpressions.Regex.Replace(a, "[^\d]", "")
            b = System.Text.RegularExpressions.Regex.Replace(b, "\s+", " ")

    LVL 10

    Assisted Solution

    Imports System.Text.RegularExpressions
    Imports System.Text

    Public Function GetOnlyNumbers(Value As String) As String
      Dim sb As New StringBuilder

      For Each match As Match in RegEx(Value, "\d")
      Next match
      Return sb.ToString()
    End Function

    ?GetOnlyNumbers("adg3234 asdfafd sadf4355")

    LVL 4

    Assisted Solution

    Instead of using RegEx, you can simply iterate through the characters of the string and check each one:

        Public Function GetOnlyNumbers(ByVal sValue As String) As String
            Dim sb As New StringBuilder
            For Each c As Char In sValue
                If Char.IsDigit(c) Then sb.Append(c)
            Next c
            Return sb.ToString
        End Function
    LVL 96

    Assisted Solution

    by:Bob Learned
    Simple way:

    Dim number As Double = Val(text)

    LVL 2

    Accepted Solution

    Hello cjinsocal581,
           The best way to do it by this :

             Dim str as string = "aabb2ukjh456lkjfc87y345"
    dim finalstr as string = string.empty

          For i = 1 To lstr.Length
                strchar = Mid(line, i, 1)
                   if isnumeric(strchar) then
                      finalstr & = strchar
                   end if
             end try            
         Next i
    LVL 4

    Expert Comment


    Featured Post

    Enabling OSINT in Activity Based Intelligence

    Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

    Join & Write a Comment

    This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
    If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
    To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    755 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    21 Experts available now in Live!

    Get 1:1 Help Now