How to replace non-numeric characters in a text string?

In vb.net, I am importing a phone number text field, and I need to output it as a field of all numerics.  Quite often the input will have hyphens that I need to eliminate, so I've been handling that with:
StrJ=StrJ.Replace("-", "")    which works fine.

But now I find that all kinds of other stuff is coming in, such as parentheses, asterisks, extra spaces, and even letters.  Is there an easy way to eliminate any and all characters that are not numeric?  I need to have a clean output that is nothing but the digits in the phone number.

LVL 3
sasllcAsked:
Who is Participating?
 
thekngConnect With a Mentor Commented:
If Not IsNumeric(Text1.Text) then
....
end if

thekng
0
 
sasllcAuthor Commented:
OK, I understand the concept (not isnumeric), but I need more detail on how to implement it (I'm new at vb).

Say I've got this string coming in that I'm calling StrJ, and it contains: 1-731-8227 (Joe)

And I want to end up with: 17318227

It's fine with me if it ends up still in StrJ, or in a different string name if that's easier.

With that in mind, how would I implement your concept above?  If possible, please give me a detailed example...

0
 
ShauliConnect With a Mentor Commented:
Private Sub Command1_Click()
Dim c As Integer, txtPos As String, StrJ As String

StrJ = "(800)555-1212"

For c = 1 To (Len(StrJ) - 1)
    txtPos = Mid(StrJ, c, 1)
    If Not txtPos = "" Then
        If Asc(txtPos) < 48 Or Asc(txtPos) > 57 Then
            StrJ = Replace(StrJ, txtPos, "")
        End If
    End If
Next c
MsgBox StrJ
End Sub

S
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
David LeeConnect With a Mentor Commented:
This'll work.

    Dim strJ, strK, strTemp As String
    Dim intCounter As Integer
    For intCounter = 1 To Len(strJ)
        strTemp = Mid(strJ, intCounter, 1)
        strK = strK & IIf(IsNumeric(strTemp), strTemp, "")
    Next
0
 
AlexFMConnect With a Mentor Commented:
Function RemoveNonDigits(ByVal s As String) As String
    Dim regex As New System.Text.RegularExpressions.Regex("[^\d]")
    RemoveNonDigits = regex.Replace(s, "")
End Function
0
 
DabasConnect With a Mentor Commented:
Hi sasllc:
You have quite a few good answers here!
Alex makes use of Regex and it might be the hardest to understand, but is worth researching!
Regular Expressions are just what you need for this kind of application

Try the following PAQ for a few good links to tutorials.
http://www.experts-exchange.com/Programming/Programming_Languages/Dot_Net/Q_20867805.html

Dabas
0
 
Mike TomlinsonConnect With a Mentor Middle School Assistant TeacherCommented:
Here is one way to do it with a Select Case statement.   I broke up the numbers into different Case sections just to show that you could do different things based upon what the current character was.  (In this scenario all the Case statements do the same thing.)

Idle_Mind

    Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        TextBox1.Text = removeNonNumericChars(TextBox1.Text)
    End Sub

    Private Function removeNonNumericChars(ByVal inputString As String) As String
        Dim outputString As String
        Dim c As Integer
        Dim ch As String

        For c = 0 To inputString.Length - 1
            ch = inputString.Substring(c, 1)
            Select Case ch
                Case "0", "1", "2", "3"
                    outputString = outputString.Concat(outputString, ch)

                Case "4", "5", "6"
                    outputString = outputString.Concat(outputString, ch)

                Case "7", "8", "9"
                    outputString = outputString.Concat(outputString, ch)

                Case Else
                    ' Do something else here...

            End Select
        Next

        Return outputString
    End Function
0
 
dancebertConnect With a Mentor Commented:
Well, we don't have an example using recursion yet ;-)

Public Function ExtractNumbers(p_sNumericString As String) As String

    '* Takes p_sNumericString and returns either:
    '*   #######...    -or-
    '*   empty string
    ' Uses recursion
   
    If p_sNumericString <> "" Then
        Trim p_sNumericString
        If IsNumeric(Left(p_sNumericString, 1)) Then
            ExtractNumbers = ExtractNumbers & _
            Left(p_sNumericString, 1) & _
            ExtractNumbers(Right(p_sNumericString, Len(p_sNumericString) - 1))
        Else
            ExtractNumbers = ExtractNumbers & _
            ExtractNumbers(Right(p_sNumericString, Len(p_sNumericString) - 1))
        End If
    Else
        Exit Function
    End If
End Function


0
 
Dave BrettConnect With a Mentor Vice President - Business EvaluationCommented:
Hi all,

I noted Alex's excellent function with interest

Function RemoveNonDigits(ByVal s As String) As String
    Dim regex As New System.Text.RegularExpressions.Regex("[^\d]")
    RemoveNonDigits = regex.Replace(s, "")
End Function

I've used RegExp extensively in Excel and not VB so I not familiar with the way that Alex has dimensioned the regex objectt but normally unless the Regex pattern is defined as global it will remove only the first match.

1-731-8227 (Joe)
would become
1731-8227 (Joe)

In Excel I would do something like

Sub a()
    Dim reg
    stra = "1-731-8227 (Joe)"
    Set reg = CreateObject("vbscript.regexp")
    reg.Pattern = "[^\d]+"
    reg.Global = True
    MsgBox reg.Replace(stra, "")
End Sub

Cheers

Dave
0
 
santosh26676Connect With a Mentor Commented:
Try this code - This will remove any non-numeric chars from the string using recursive calls:

' Paste this code in Form Load Event
Private Sub Form_Load()
    Dim strj As String
   
    strj = "(044)-20-7493 4033"
   
    RemoveInvalidChars strj, 1
   
    MsgBox strj
End Sub

' Function to remove non-numeric chars (called recursively)
Private Function RemoveInvalidChars(stra As String, startat As Integer)
    If startat > Len(stra) Then Exit Function
   
    If Not IsNumeric(Mid(stra, startat, 1)) Then
        stra = Replace(stra, Mid(stra, startat, 1), "")
    End If
    startat = startat + 1
   
    RemoveInvalidChars stra, startat
End Function
0
All Courses

From novice to tech pro — start learning today.