Solved

In vb.net, how to eliminate non-numbers in a string?

Posted on 2016-10-07
3
41 Views
Last Modified: 2016-10-12
The string should have nothing but numbers in it, but if it does, I need to find out if there is an easy way to eliminate the non-numeric characters.

I ran into a situation where there was a "#" in the field, so for that situation I used this code:

psWork1 = psWork1.Replace("#", "")

...and that worked great.  But I need code that will handle ANY non-numbers in the string.  TIA
0
Comment
Question by:sasllc
3 Comments
 
LVL 12

Accepted Solution

by:
funwithdotnet earned 200 total points
ID: 41834274
Here's a helper method I use:
                ''' <summary>
                ''' Filters non-numeric characters from a String.
                ''' </summary>
                ''' <param name="input">The String value to be filtered.</param>
                ''' <returns>Returns only the numeric characters from the supplied String.</returns>
                Public Shared Function NumericFilter(ByVal input As String) As String
                    Dim output As String = ""
                    If input.Length > 0 Then
                        Dim tempChar() As Char = input.ToCharArray()
                        For Each myChar As Char In tempChar
                            If Char.IsDigit(myChar) Then
                                output &= myChar.ToString
                            End If
                        Next
                    End If
                    Return output
                End Function

Open in new window

0
 
LVL 26

Assisted Solution

by:Shaun Kline
Shaun Kline earned 150 total points
ID: 41834279
You could use a Regular Expression to do this. Your matching pattern would be a negation of the acceptable characters:
Imports System.Text.RegularExpression

psWork1 = Regex.Replace(psWork1, "[^0-9]", "")

Open in new window

The [^0-9] in the code above should equate to all characters except 0-9.
0
 
LVL 32

Assisted Solution

by:it_saige
it_saige earned 150 total points
ID: 41834291
Couple of ways, look at each character in the string and determine if it is numeric, if it is, keep it or using regular expressions to identify the characters you want to keep  and replacing all others:
Imports System.Text.RegularExpressions

Module Module1
	Sub Main()
		Const stripthis = "Th1s 1s 4 str1ng w1th ch4r4ct3rs fr0m wh1ch w3 0nly w4nt th3 n#mb3rs."

		Console.WriteLine("Using a for loop: {0}", UsingForLoop(stripthis))
		Console.WriteLine("Using a foreach loop: {0}", UsingForEachLoop(stripthis))
		Console.WriteLine("Using linq: {0}", UsingLinq(stripthis))
		Console.WriteLine("Using regular expressions: {0}", UsingRegEx(stripthis))
		Console.ReadLine()
	End Sub

	Function UsingForLoop(source As String) As String
		Dim result = String.Empty
		For i = 0 To source.Length - 1
			If IsNumeric(source(i)) Then
				result += source(i)
			End If
		Next
		Return result
	End Function

	Function UsingForEachLoop(source As String) As String
		Dim result = String.Empty
		For Each c As Char In source
			If IsNumeric(c) Then
				result += c
			End If
		Next
		Return result
	End Function

	Function UsingLinq(source As String) As String
		Return New String((From c As Char In source Where IsNumeric(c) Select c).ToArray())
	End Function

	Function UsingRegEx(source As String) As String
		Return Regex.Replace(source, "[^0-9]", "")
	End Function
End Module

Open in new window


Produces the following output -Capture.JPG
-saige-
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now