Click here to Skip to main content
Click here to Skip to main content
Alternative Tip/Trick

How to Remove Characters from a Numeric String

, 7 Jul 2011 CPOL
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

  • 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

QuestionThis is not even a tip PinprofessionalRage1-Apr-14 7:09 
AnswerRe: This is not even a tip PinprofessionalPrerak Patel1-Apr-14 19:58 
GeneralReason for my vote of 5 short and sweet trick, I always love... PinmemberHiren Solanki30-Aug-11 2:37 
GeneralReason for my vote of 5 More concise PinmemberCatalin Hatmanu8-Jul-11 11:59 

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

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

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