Trim everything BUT numbers from a string.

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.
LVL 6
cjinsocal581Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ericwong27Commented:
       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+", " ")

0
bchoorCommented:
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")
    sb.Append(match.Value)
  Next match
  Return sb.ToString()
End Function

Usage:
?GetOnlyNumbers("adg3234 asdfafd sadf4355")
"32344355"

HTH
~BC
0
YetiNZCommented:
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
0
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

Bob LearnedCommented:
Simple way:

Dim number As Double = Val(text)

Bob
0
manojkumarpsCommented:
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)
         try
               if isnumeric(strchar) then
                  finalstr & = strchar
               end if
         catch
         end try            
     Next i
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.