By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,326 Members | 881 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,326 IT Pros & Developers. It's quick & easy.

Find only digits in string

P: n/a
How to filter out only digits from a string?
i.e. fgfdg435fdg7dgf5:df,54 => 4357554
Thanks

Nov 21 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
CT
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)
--
Carsten Thomsen
Enterprise Development with VS .NET, UML, AND MSF
http://www.apress.com/book/bookDisplay.html?bID=105
Communities - http://community.integratedsolutions.dk

"Nikolay Petrov" <jo******@mail.bg> wrote in message
news:11**********************@g44g2000cwa.googlegr oups.com...
How to filter out only digits from a string?
i.e. fgfdg435fdg7dgf5:df,54 => 4357554
Thanks

Nov 21 '05 #2

P: n/a
CT
Oops, forgot the RegEx example:

numbers = Regex.Replace(full, "[aA-zZ]+", "")
--
Carsten Thomsen
Enterprise Development with VS .NET, UML, AND MSF
http://www.apress.com/book/bookDisplay.html?bID=105
Communities - http://community.integratedsolutions.dk

"CT" <ca******@spammersgoawaydotnetservices.biz> wrote in message
news:OE*************@tk2msftngp13.phx.gbl...
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)
--
Carsten Thomsen
Enterprise Development with VS .NET, UML, AND MSF
http://www.apress.com/book/bookDisplay.html?bID=105
Communities - http://community.integratedsolutions.dk

"Nikolay Petrov" <jo******@mail.bg> wrote in message
news:11**********************@g44g2000cwa.googlegr oups.com...
How to filter out only digits from a string?
i.e. fgfdg435fdg7dgf5:df,54 => 4357554
Thanks


Nov 21 '05 #3

P: n/a
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
---------------------
"CT" <ca******@spammersgoawaydotnetservices.biz> wrote in message
news:OE*************@tk2msftngp13.phx.gbl...
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)
--
Carsten Thomsen
Enterprise Development with VS .NET, UML, AND MSF
http://www.apress.com/book/bookDisplay.html?bID=105
Communities - http://community.integratedsolutions.dk

"Nikolay Petrov" <jo******@mail.bg> wrote in message
news:11**********************@g44g2000cwa.googlegr oups.com...
How to filter out only digits from a string?
i.e. fgfdg435fdg7dgf5:df,54 => 4357554
Thanks


Nov 21 '05 #4

P: n/a
"Nikolay Petrov" <jo******@mail.bg> schrieb:
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]", ""))
///

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Nov 21 '05 #5

P: n/a
I'll use Ken's example, bucause of sum UNICODE chars in my string.
Thanks to all

Nov 21 '05 #6

P: n/a
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.Reg ex.Replace(s, "[^0-9]",
""))
///

I hope this helps,

Cor

"Nikolay Petrov" <jo******@mail.bg> schreef in bericht
news:11**********************@g44g2000cwa.googlegr oups.com...
I'll use Ken's example, bucause of sum UNICODE chars in my string.
Thanks to all

Nov 21 '05 #7

P: n/a
Thanks Cor
awesome

Nov 21 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.