Atlanta Custom Software Development 

 
   Search        Code/Page
 

User Login
Email

Password

 

Forgot the Password?
Services
» Web Development
» Maintenance
» Data Integration/BI
» Information Management
Programming
  Database
Automation
OS/Networking
Graphics
Links
Tools
» Regular Expr Tester
» Free Tools

Faster string comparisons with CompareOrdinal

Total Hit ( 3651)

Rate this article:     Poor     Excellent 

 Submit Your Question/Comment about this article

Rating


 


You can compare strings in many ways under VB.NET: by using the = operator (or another comparison operator), with the Equals method (which the String class inherits from System.Object), or with the Compare shared method. The Compare method is especially useful because it returns an integer that tells whether the first string is less (-1), equal (0), or higher (+1) than the second string. This feature lets you perform only one comparison and use the result in a Select Case block:

Click here to copy the following block
Select Case String.Compare(first, second)
  Case -1
    Console.WriteLine("{0} is less than {1}", first, second)
  Case 0
    Console.WriteLine("{0} is equal to {1}", first, second)
  Case 1
    Console.WriteLine("{0} is higher than {1}", first, second)
End Select

The only problem with the Compare method is that it compares strings in a local-aware fashion, so it has to convert the Unicode code of each character into a numeric value that reflects its position in the current culture's alphabet. For example, the Compare method considers the "b" lowercase character to come immediately after the "A" uppercase char and before the "B" uppercase char, even though the "A" and "B" characters are contiguous in the Unicode character set. This conversion activity takes time and consume CPU cycles, so you'll find that VB.NET is less efficient than VB6 at comparing strings. Using the = operator and other comparison operators doesn't help at all, because they map to the Compare method behind the scenes, so these operators suffer from the same performance loss.
If you are only interested in checking whether two strings contain the same characters (in a case-sensitive comparison), you can speed up your code by using the CompareOrdinal shared method. This method is 3-4 times faster than the Compare method (or the = operator) because it just scans the two strings and compare the Unicode numeric code of each character:

Click here to copy the following block
If String.CompareOrdinal(first, second) = 0 Then
  Console.WriteLine("The two strings are equal")
Else
  Console.WriteLine("The two strings are different")
End If


Submitted By : Nayan Patel  (Member Since : 5/26/2004 12:23:06 PM)

Job Description : He is the moderator of this site and currently working as an independent consultant. He works with VB.net/ASP.net, SQL Server and other MS technologies. He is MCSD.net, MCDBA and MCSE. In his free time he likes to watch funny movies and doing oil painting.
View all (893) submissions by this author  (Birth Date : 7/14/1981 )


Home   |  Comment   |  Contact Us   |  Privacy Policy   |  Terms & Conditions   |  BlogsZappySys

© 2008 BinaryWorld LLC. All rights reserved.