Solved

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

Posted on 2004-04-03
10
1,130 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
Comment Utility
If Not IsNumeric(Text1.Text) then
....
end if

thekng
0
 
LVL 3

Author Comment

by:sasllc
Comment Utility
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
Comment Utility
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
 
LVL 76

Assisted Solution

by:David Lee
David Lee earned 50 total points
Comment Utility
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
Comment Utility
Function RemoveNonDigits(ByVal s As String) As String
    Dim regex As New System.Text.RegularExpressions.Regex("[^\d]")
    RemoveNonDigits = regex.Replace(s, "")
End Function
0
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.

 
LVL 27

Assisted Solution

by:Dabas
Dabas earned 50 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

762 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

6 Experts available now in Live!

Get 1:1 Help Now