Find only digits in string

  • Thread starter Thread starter Nikolay Petrov
  • Start date Start date
N

Nikolay Petrov

How to filter out only digits from a string?
i.e. fgfdg435fdg7dgf5:df,54 => 4357554
Thanks
 
There are several ways, including using a regular expression. You can also
do this:

Dim full As String = "jhg345gvbjk36hg53j5j3hg5j3"
Dim validChars As String = "0123456789"
Dim numbers As String
Dim allChars() As Char = full.ToCharArray

For Each ch As Char In allChars
If validChars.IndexOf(ch) <> -1 Then
numbers &= ch
End If
Next

MessageBox.Show(numbers)
 
Oops, forgot the RegEx example:

numbers = Regex.Replace(full, "[aA-zZ]+", "")
 
Hi,

I would use the char's isdigit method instead.

For Each ch As Char In allChars
If Char.IsDigit(ch) Then
numbers &= ch
End If
Next


Ken
---------------------
 
Nikolay Petrov said:
How to filter out only digits from a string?
i.e. fgfdg435fdg7dgf5:df,54 => 4357554

\\\
Dim s As String = "&A$§23zb4-u9%%8Z"
MsgBox(Regex.Replace(s, "[^0-9]", ""))
///
 
I'll use Ken's example, bucause of sum UNICODE chars in my string.
Thanks to all
 
Nikolay,

Than I see a nice implementation given by Herfried from the Regex and than
you are not using it.

However he did not give you the complete reference that is needed.
\\\
Dim s As String = "&A$§23zb4-u9%%8Z"
MessageBox.Show(System.Text.RegularExpressions.Regex.Replace(s, "[^0-9]",
""))
///

I hope this helps,

Cor
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top