Solved

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

Posted on 2004-04-03
10
1,136 Views
Last Modified: 2008-09-23
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.

0
Comment
Question by:sasllc
10 Comments
 
LVL 1

Accepted Solution

by:
thekng earned 100 total points
ID: 10748555
If Not IsNumeric(Text1.Text) then
....
end if

thekng
0
 
LVL 3

Author Comment

by:sasllc
ID: 10748629
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
 
LVL 19

Assisted Solution

by:Shauli
Shauli earned 50 total points
ID: 10748687
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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
LVL 76

Assisted Solution

by:David Lee
David Lee earned 50 total points
ID: 10748717
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
 
LVL 48

Assisted Solution

by:AlexFM
AlexFM earned 50 total points
ID: 10748913
Function RemoveNonDigits(ByVal s As String) As String
    Dim regex As New System.Text.RegularExpressions.Regex("[^\d]")
    RemoveNonDigits = regex.Replace(s, "")
End Function
0
 
LVL 27

Assisted Solution

by:Dabas
Dabas earned 50 total points
ID: 10749241
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
 
LVL 85

Assisted Solution

by:Mike Tomlinson
Mike Tomlinson earned 50 total points
ID: 10749578
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
 
LVL 9

Assisted Solution

by:dancebert
dancebert earned 50 total points
ID: 10749615
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
 
LVL 50

Assisted Solution

by:Dave Brett
Dave Brett earned 50 total points
ID: 10749708
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
 
LVL 3

Assisted Solution

by:santosh26676
santosh26676 earned 50 total points
ID: 10754916
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

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…

813 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

18 Experts available now in Live!

Get 1:1 Help Now