Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / VB.NET / October 2004

Tip: Looking for answers? Try searching our database.

Faster method of validating a string for all digits

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Tim Frawley - 27 Oct 2004 01:25 GMT
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?
jjardine - 27 Oct 2004 01:32 GMT
>I have a method for testing variable length strings for all digits:
>
[quoted text clipped - 14 lines]
> 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??
David Browne - 27 Oct 2004 01:52 GMT
>>I have a method for testing variable length strings for all digits:
>>
[quoted text clipped - 16 lines]
>
> 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
Gerald Hernandez - 27 Oct 2004 02:13 GMT
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

> >>I have a method for testing variable length strings for all digits:
> >>
[quoted text clipped - 21 lines]
>
> David
Tim Frawley - 27 Oct 2004 02:22 GMT
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.
Tim Frawley - 27 Oct 2004 02:12 GMT
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).
David Browne - 27 Oct 2004 01:49 GMT
>I have a method for testing variable length strings for all digits:
>
[quoted text clipped - 14 lines]
> 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
Gerald Hernandez - 27 Oct 2004 02:07 GMT
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

> I have a method for testing variable length strings for all digits:
>
[quoted text clipped - 14 lines]
> 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 - 27 Oct 2004 02:22 GMT
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
Gerald Hernandez - 27 Oct 2004 02:41 GMT
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

> 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
Ken Tucker [MVP] - 27 Oct 2004 04:14 GMT
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?
David Browne - 27 Oct 2004 14:31 GMT
> Hi,
>
[quoted text clipped - 5 lines]
>
> 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
Tim Frawley - 27 Oct 2004 20:42 GMT
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

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.