count occurances of digits in a string

  • Thread starter Thread starter Smokey Grindle
  • Start date Start date
S

Smokey Grindle

any fast way to do this? I just need to check if if a string which could
have a lot of characters in it if it has numbers and count the number of
numbers in the string. thanks!
 
Smokey said:
any fast way to do this? I just need to check if if a string which could
have a lot of characters in it if it has numbers and count the number of
numbers in the string. thanks!

Use the Matches method in the Regex class with a regular expression
pattern as "\d+" to find all occurances of digits.

Depending on if you want to count numbers or count digits (which isn't
really clear), get the number of matches or add the lengths of the matches.
 
any fast way to do this? I just need to check if if a string which could
have a lot of characters in it if it has numbers and count the number of
numbers in the string. thanks!

I thinking something like this:

' Assumes Imports System.Text.RegularExpressions

Dim s As String = "Some string 454 with numbers in it. 4234"
Dim regex As New Regex("\d")
MsgBox(regex.Matches(s).Count)

Thanks,

Seth Rowe
 
Back
Top