Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / web / ASP.NET

How to Remove Characters from a Numeric String

3.60/5 (9 votes)
7 Jul 2011CPOL 63.9K  
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)