Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

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

Posted on 2004-04-03
10
Medium Priority
?
1,171 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 400 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 200 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 76

Assisted Solution

by:David Lee
David Lee earned 200 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 200 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 200 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 200 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 200 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 200 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 200 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
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…
Suggested Courses

810 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