Click here to Skip to main content
12,752,876 members (38,339 online)
Click here to Skip to main content
Add your own
alternative version

Stats

11.6K views
Posted 5 Jul 2011

How to remove characters from a Numeric String:

, 5 Jul 2011 CPOL
Rate this:
Please Sign up or sign in to vote.
How to remove characters from a Numeric String:
facing issues to know how do we remove characters from a String?
Consider the following example for mobile number cleanup, Take a textbox on the form
Rename it to ="TxtMobile"
Add a label and rename it to"LblResult"
Add a label and rename it to "CmdSubmit"

Declare a public string
Public Mobile As String


Under "CmdSubmit" click write the following code:
Protected Sub CmdSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles CmdSubmit.Click
       If Not TxtMobile.Text = "" Then
           LblResult.Text = ""
           Mobile = TxtMobile.Text
           CheckforChars()
           Lblresult.Text = Mobile
       Else
           Label1.Text = "Enter Mobile Number with some characters"
       End If
   End Sub

The above code assigns textbox value to public string we have declare above and passes it to sub CheckforChars(), Then the results are shown in Lblrsult.
Aslo write two small functions to remove characters from string of numeric

The below functions removes all the digits or numeric values from a string and stores all the chacters
Public Shared Function RemoveDigits(ByVal key As String) As String
       Return Regex.Replace(key, "\d", "")
   End Function


The Mobile number with characters is passed through the above function in the below sub and returned value from the above function is stored in a string called Value1
The characters present in Mobile number are replaced with nothing, you can also use .remove here instead of replace
Private Sub CheckforChars()
        Dim value1 As String = RemoveDigits(Mobile)
        Mobile = Mobile.Replace(value1, "")
    End Sub



Cheers:)
SYed WAyez

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Syed Wayez Ahmed
Software Developer (Senior) Saudi Hollandi Bank
Saudi Arabia Saudi Arabia
Enjoy Work, Celebrate Success and learn from the Experience
Am a Middleware developer working mainly on building interfaces have been programming in vb.net and C# since last 3 yrs and some mnths...

You may also be interested in...

Comments and Discussions

 
GeneralReason for my vote of 1 Bad idea, bad code. Try your code wi... Pin
Prerak Patel7-Jul-11 2:51
mvpPrerak Patel7-Jul-11 2:51 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170217.1 | Last Updated 5 Jul 2011
Article Copyright 2011 by Syed Wayez Ahmed
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid