Faster method of validating a string for all digits

  • Thread starter Thread starter Tim Frawley
  • Start date Start date
T

Tim Frawley

I have a method for testing variable length strings for all digits:

Dim ch As String
Dim i As Integer

AllDigits = True
For i = 1 To Len(txt)
' See if the next character is a non-digit.
ch = Mid$(txt, i, 1)
If ch < "0" Or ch > "9" Then
' This is not a digit.
AllDigits = False
Exit For
End If
Next i


Does anyone know of a faster method in vb.net that does not require
looping the length of the string until an invalid character is found?
 
Tim Frawley said:
I have a method for testing variable length strings for all digits:

Dim ch As String
Dim i As Integer

AllDigits = True
For i = 1 To Len(txt)
' See if the next character is a non-digit.
ch = Mid$(txt, i, 1)
If ch < "0" Or ch > "9" Then
' This is not a digit.
AllDigits = False
Exit For
End If
Next i


Does anyone know of a faster method in vb.net that does not require
looping the length of the string until an invalid character is found?

how about the functin isNumeric??
 
Tim Frawley said:
I have a method for testing variable length strings for all digits:

Dim ch As String
Dim i As Integer

AllDigits = True
For i = 1 To Len(txt)
' See if the next character is a non-digit.
ch = Mid$(txt, i, 1)
If ch < "0" Or ch > "9" Then
' This is not a digit.
AllDigits = False
Exit For
End If
Next i


Does anyone know of a faster method in vb.net that does not require
looping the length of the string until an invalid character is found?

Well, I don't know a method which doesn't loop through the characters, a
couple simple changes will make your procedure much faster (30x in my test).
Plus it generates no garbage.

Function IsNumeric(ByVal txt As String) As Boolean
Dim ch As Char
Dim len As Integer = txt.Length - 1
Dim i As Integer

For i = 0 To len
' See if the next character is a non-digit.
ch = txt.Chars(i)
If ch < "0"c Or ch > "9"c Then
' This is not a digit.
Return False
Exit For
End If
Next i
Return True

End Function


David
 
jjardine said:
how about the functin isNumeric??

It's OK. Its about 3x faster than the method posted, but still 10x slower
that simply iterating the chars using String.Char(integer).

David
 
You have probably found the IsNumeric function already, yes?
If so, you have also probably discovered it is not what you want.

I don't know about performance, but this one sure has minimal code.
It is out of a large collection of string operators I use all the time.

Function IsStringAllNumeric(ByVal TestString As String) As Boolean
If TestString <> vbNullString Then
Return Not (TestString Like "*[!0-9]*")
Else
Return False
End If
End Function

Gerald
 
The isNumeric function will return true if the number is:

-32
1.2

The value I am validating can only be 0 to 9 (digits).
 
And keep in mind that IsNumeric will attempt to evaluate the string
expression as a number.
If you want to make sure only digits exist, this does not work.
For example, IsNumeric would return True for these:
"12", "12.3", "12,3", "&HBAD", "&O123"

Gerald
 
David,

I will run the app through our profiler tomorrow to see how much of a
bottle neck your function is.

On another note, the KeyPress event has a good one: e.KeyChar.IsDigit

Unfortnately I am not validating key press events. :)

I will have to validate data submissions possibly up to 1 million rows
and I was just hoping someone knew of a faster method.

Thanks for the reply.
 
Thanks Gerald, that looks interesting.

I will run it through our profiler tomorrow and see how it goes. I will
post back my findings if anyone is interested.

Tim
 
I'm interested.
Over the years I have worked my way down to this one.
Processed many dozens of millions of records.
I haven't looked at the overall performance of this in dotNet.
It is a direct port from VB6 that has worked very reliably.

Gerald
 
Hi,


Use a regex. This will check the text in textbox1 to see if it
is a number.

Dim regNum As New
System.Text.RegularExpressions.Regex("^(\d|-)?(\d|,)*\.?\d*$")

MessageBox.Show(regNum.IsMatch(TextBox1.Text).ToString)



Ken

---------------------------------

I have a method for testing variable length strings for all digits:

Dim ch As String
Dim i As Integer

AllDigits = True
For i = 1 To Len(txt)
' See if the next character is a non-digit.
ch = Mid$(txt, i, 1)
If ch < "0" Or ch > "9" Then
' This is not a digit.
AllDigits = False
Exit For
End If
Next i


Does anyone know of a faster method in vb.net that does not require
looping the length of the string until an invalid character is found?
 
Ken Tucker said:
Hi,


Use a regex. This will check the text in textbox1 to see if it
is a number.

Dim regNum As New
System.Text.RegularExpressions.Regex("^(\d|-)?(\d|,)*\.?\d*$")

MessageBox.Show(regNum.IsMatch(TextBox1.Text).ToString)

regex is certianly a good solution. It's fast, and it saves you from having
to code procedurally. But regex is not the fastest method. My tests show
that this method

Function isNumeric(ByVal txt As String) As Boolean
Static regNum As New Regex("\d*",
Text.RegularExpressions.RegexOptions.Compiled)
Return regNum.IsMatch(txt)
End Function

is about as fast as the built-in isNumeric() function, and 15x slower that
just examining each char.

David
 
Well the results are in.

First off I am using Red Ants Profiler 1.22

I have 65,535 rows wherein I validated only one column that contains a
datavalue of two characters. Both digits in this case. This way the
three functions I tried all returned true for that column.

Ken, with the "RegularExpression.Regex" had the slowest time of 1.87
seconds.

Gerald came in a close second with his function "Return Not (TestString
Like "*[!0-9]*")" for a time of .463 seconds.

David function was the winner looping the string for each character for
a time of .176 seconds.

I ran the profiler several times changing only the function calls after
each test. These results are from the last test run. All tests showed
a distinct differnce in time with Davids method as the fastest.

As you can see from all these tests though my bottle neck is certainly
not in testing for AllDigits!

I want to thank you all for your suggestions! This was kind of fun.
There should be a website that specializes in this type of testing eh?

Again, Thank you all, David, Gerald, and Ken.


Sincerely,


Tim Frawley
 

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