Click here to Skip to main content
15,867,330 members
Articles / Web Development / ASP.NET
Tip/Trick

How to remove characters from a Numeric String:

Rate me:
Please Sign up or sign in to vote.
2.50/5 (2 votes)
5 Jul 2011CPOL 17.6K   1
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)


Written By
Software Developer (Senior) Saudi Hollandi Bank
Saudi Arabia Saudi Arabia
This member doesn't quite have enough reputation to be able to display their biography and homepage.

Comments and Discussions

 
GeneralReason for my vote of 1 Bad idea, bad code. Try your code wi... Pin
Prerak Patel7-Jul-11 1:51
professionalPrerak Patel7-Jul-11 1: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.