Comparing objects has always been a tricky affair. Luckily, with each new version of the .NET Framework Microsoft makes things a little bit easier. With the advent of the .NET Framework, many moons ago, a lot of new namespaces were created and a lot more effort was put into making comparing the equality of two or more objects easier.
Before I get too technical, let me get back to the basics and explain Strings more properly:
The term ‘Strings’ always seem to amount to some sort of headache among new programmers. We all know what a string is: It is any set of alphanumeric input. The trick comes in knowing how to manipulate strings properly. Once you have learned this skill, you’re halfway to becoming a great programmer!
What String Functions Are There?
The key to working with strings is to know what functions are available. There are actually many string functions available to Visual Basic developers. A more detailed list of all of the string functions can be found at this link.
The CaseInsensitiveComparer class does case-insensitive comparison on string objects. CaseInsensitiveComparer implements the IComparer interface that supports case-insensitive comparisons on strings. For more information on CaseInsensitiveComparer, have a look here.
String comparisons might have different results, depending on the culture.
Globalization is basically the process of making your application culture-friendly. You have to realize that the users of your application may speak a different language than you, and therefore their language adheres to different rules, apart from being a different language. For example, some languages are displayed left to right, while others should be displayed right to left. Now, you have to not only think about the different language, but different currencies, date, and time settings as well as different colors being used with different cultures.
With Globalization, you can make your application useable with any culture irrespective of its language.
Here are some more articles on Globalization:
Localization allows you to customize your application according to a certain culture.
Here is more information on Localization.
The IComparer Interface simply exposes a method that compares two objects. For more information regarding the IComparer Interface, have a look here.
Start Visual Studio and create a new Visual Basic Console Application. Add the following into your Module:
Imports System.Globalization Imports System.Collections Imports System.Collections.Specialized Class Program Shared Sub Main(ByVal args() As String) ' Make the dictionary case insensitive Dim list as New ListDictionary( _ New CaseInsensitiveComparer(CultureInfo.InvariantCulture)) ' Add some items list("De Verenigde Staten van Amerika") = _ "United States of America" list("Canada") = "Kanada" list("España") = "Spanje" ' Show the results Console.WriteLine(list("españa")) Console.WriteLine(list("CANADA")) Console.Read() End Sub End Class
This code basically translates various country names to Dutch and then does a case-insensitive comparison between the given strings.
There is always much more than meets the eye using any technology, especially with string comparisons.