Solved

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

Posted on 2004-04-03
10
1,142 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 86

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
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 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…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

726 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