Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 97
  • Last Modified:

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

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
sasllc
Asked:
sasllc
3 Solutions
 
funwithdotnetCommented:
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
 
Shaun KlineLead Software EngineerCommented:
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
 
it_saigeDeveloperCommented:
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now