So, you know everything about strings, right?–part IV
String comparison is one of the most common operations you’ll need to perform in your daily tasks. Besides checking for equality, you’ll also end up comparing strings whenever you need to sort them. The String type offers several methods for performing comparison operations:
- Equals: this instance method checks two strings for equality, according with the StringComparison enum value that it receives. There’s also a static version of this method which lets you use the operation without having to check for null.
- Compare: the type introduces several static overloads of this method which can be used to sort two strings.
- StartsWith: String introduces two overloads of this instance method. You can use this method to check if the current string starts with another that it receives through a parameter.
- EndsWith: once again, there are two overloads of this method. You can use them to see if the current string ends up with another passed in through a parameter.
Most of these methods can receive a StringComparison value through a parameter. This value lets you influence the comparison operation by specifying if the comparison should be case sensitive or insensitive and it the current culture should be used in the current operation.
Some of the overloads of the previous method can also receive a value from the CompareOptions enum. Since this is a flag enumeration, you can combine several of its values. Most of the comparison operations can be influenced through a CultureInfo object. That’s why there are several overloads which let you specify the culture that should be used on the comparison operation.
In practice, you can divide comparison operations into two big groups: programmatic string comparisons and linguistic string comparisons. Programmatic string comparisons include all string comparisons related with…you guesses it: programming! For instance, whenever you compare paths, registry keys, URLs, etc., you’re running a programmatic string comparison. The fastest way (and probably the best) of doing this is to run an ordinal case insensitive comparison. Whenever you do this, you’re running a fast comparison because the culture info isn’t taken into account by the method. In practice, this means passing the StringComparison.Ordinal or StringComparison.OrdinalIgnoreCase value to the method. You shouldn’t use the InvariantCulture and InvariantCultureIgnoreCase values because they are more costly (from a time/performance point of view) than using the previous values.
There are, however, several times when you do need to compare Strings entered by the user or which are shown to him. In these cases, we’re talking about linguistic string comparisons and they should always take the current culture into account. If you’re sorting strings, you should also perform these operations in a case sensitive way to ensure that two strings which only differ by case aren’t considered equal (if they are, then sorting them might return different orderings in several sort operations and that might really confuse the user). You perform a linguistic string comparison by passing the value StringComparison.CurrentCulture or StringComparison.CurrentCultureIgnoreCase value.
As I’ve mentioned, linguistic string operations depend on the culture. And that’s why we need to take a little detour into cultures before going on. Since this is becoming a rather large post, I’ll leave the details about cultures for a future post. Stay tuned for more!