Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 271
  • Last Modified:

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.
0
cjinsocal581
Asked:
cjinsocal581
5 Solutions
 
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now