Click here to Skip to main content
13,348,874 members (76,368 online)
Click here to Skip to main content
Add your own
alternative version


2 bookmarked
Posted 7 Jul 2011

How to Remove Characters from a Numeric String

, 7 Jul 2011
Rate this:
Please Sign up or sign in to vote.
How to remove characters from a numeric string


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.

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:


The output is:



  • 1st April, 2014: Initial version


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


About the Author

Prerak Patel
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

You may also be interested in...

Comments and Discussions

QuestionRemove character olny and let's the vergule(,.) in this NumString???? Pin
elmeksoaui127-May-16 1:30
memberelmeksoaui127-May-16 1:30 
AnswerRe: Remove character olny and let's the vergule(,.) in this NumString???? Pin
Prerak Patel29-May-16 19:01
professionalPrerak Patel29-May-16 19:01 
QuestionHow about? Pin
RendeRRR25-Aug-15 4:02
memberRendeRRR25-Aug-15 4:02 
GeneralRe: How about? Pin
Prerak Patel26-Aug-15 23:42
professionalPrerak Patel26-Aug-15 23:42 
QuestionStrip away non-numeric characters from a phone number Pin
PiyushVarma10-Jul-15 8:55
memberPiyushVarma10-Jul-15 8:55 
GeneralI just needed it really quick Pin
jkirkerx18-Mar-15 12:39
professionaljkirkerx18-Mar-15 12:39 
QuestionThis is not even a tip Pin
Rage1-Apr-14 7:09
professionalRage1-Apr-14 7:09 
AnswerRe: This is not even a tip Pin
Prerak Patel1-Apr-14 19:58
professionalPrerak Patel1-Apr-14 19:58 
GeneralReason for my vote of 5 short and sweet trick, I always love... Pin
Hiren Solanki30-Aug-11 2:37
memberHiren Solanki30-Aug-11 2:37 
GeneralReason for my vote of 5 More concise Pin
Catalin Hatmanu8-Jul-11 11:59
memberCatalin Hatmanu8-Jul-11 11: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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.180111.1 | Last Updated 7 Jul 2011
Article Copyright 2011 by Prerak Patel
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid