Click here to Skip to main content
15,868,016 members
Articles / Web Development / ASP.NET
Alternative
Tip/Trick

How to Remove Characters from a Numeric String

Rate me:
Please Sign up or sign in to vote.
3.60/5 (9 votes)
7 Jul 2011CPOL 61K   2   11
How to remove characters from a numeric string

Introduction

Regex.Replace(testnumber, "[^\d]", "") is sufficient to remove all non-numeric characters from string testnumber.

Using the Code

The following code was made in VB.NET. Read the numbers as string, and assign to testnumber. Then regular expression is applied to testnumber, expression is "[^\d]". Except numeric string, rest of characters are replaced with blank.

VB.NET
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
     Dim testnumber As String = TextBox3.Text
     MessageBox.Show(System.Text.RegularExpressions.Regex.Replace(testnumber, "[^\d]", " "))
End Sub 

The input is as follows:

SJHB*&8732{}\,./ 

The output is:

8732 

History

  • 1st April, 2014: Initial version

License

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


Written By
Software Developer
India India
Working since 2006 on VBA, VB6, VB.Net, C#, ASP.Net, MSSQL




  • Courage is not the absence of fear, but rather the judgement that something is more important than fear.
  • The fear of suffering is worse than the suffering itself.
  • People need not fear the unknown if they are capable of achieving what they need and want.
  • Every blessing ignored becomes a curse.
  • Sometimes what's in your head isn't as crazy as you think.
  • We never really grow up, we only learn how to act in public.
  • You can make very bad teams with very good individuals.
  • Admitting mistakes means you have a sense of responsibility in your actions and that shows you are more matured than almost anyone. -Nithin


Comments and Discussions

 
QuestionRemove character olny and let's the vergule(,.) in this NumString???? Pin
elmeksoaui127-May-16 0:30
elmeksoaui127-May-16 0:30 
AnswerRe: Remove character olny and let's the vergule(,.) in this NumString???? Pin
Prerak Patel29-May-16 18:01
professionalPrerak Patel29-May-16 18:01 
QuestionHow about? Pin
RendeRRR25-Aug-15 3:02
RendeRRR25-Aug-15 3:02 
GeneralRe: How about? Pin
Prerak Patel26-Aug-15 22:42
professionalPrerak Patel26-Aug-15 22:42 
QuestionStrip away non-numeric characters from a phone number Pin
PiyushVarma10-Jul-15 7:55
PiyushVarma10-Jul-15 7:55 
GeneralI just needed it really quick Pin
jkirkerx18-Mar-15 11:39
professionaljkirkerx18-Mar-15 11:39 
QuestionThis is not even a tip Pin
Rage1-Apr-14 6:09
professionalRage1-Apr-14 6:09 
AnswerRe: This is not even a tip Pin
Prerak Patel1-Apr-14 18:58
professionalPrerak Patel1-Apr-14 18:58 
GeneralReason for my vote of 5 short and sweet trick, I always love... Pin
Hiren solanki30-Aug-11 1:37
Hiren solanki30-Aug-11 1:37 
GeneralReason for my vote of 5 More concise Pin
Catalin Hatmanu8-Jul-11 10:59
Catalin Hatmanu8-Jul-11 10:59 

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.