65.9K
CodeProject is changing. Read more.
Home

How to Remove Characters from a Numeric String

starIconstarIconstarIcon
emptyStarIcon
starIcon
emptyStarIcon

3.60/5 (9 votes)

Jul 7, 2011

CPOL
viewsIcon

63906

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.

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