DekGenius.com
[ Team LiB ] Previous Section Next Section

Recipe 2.3 Controlling Case Sensitivity when Comparing Two Characters

Problem

You need to compare two characters for equality, but you need the flexibility of performing a case-sensitive or case-insensitive comparison.

Solution

Use the Equals instance method on the char structure to compare the two characters:

public static bool IsCharEqual(char firstChar, char secondChar)
{
    return (IsCharEqual(firstChar, secondChar, false));
}

public static bool IsCharEqual(char firstChar, char secondChar, 
                               bool caseSensitiveCompare)
{
    if (caseSensitiveCompare)
    {
        return (firstChar.Equals(secondChar));
    }
    else
    {
        return (char.ToUpper(firstChar).Equals(char.ToUpper(secondChar)));
    }
}

The first overloaded IsCharEqual method takes only two parameters: the characters to be compared. This method then calls the second IsCharEqual method with three parameters. The third parameter on this method call defaults to false so that when this method is called, you do not have to pass in a value for the caseSensitiveCompare parameter—it will automatically default to false.

Discussion

Using the ToUpper method in conjunction with the Equals method on the string class allows us to choose whether to take into account the case of the strings when comparing them. To perform a case-sensitive comparison of two char variables, simply use the Equals method, which, by default, performs a case-sensitive comparison. Performing a case-insensitive comparison requires that both characters be converted to their uppercase values (they could just as easily be converted to their lowercase equivalents, but for this recipe we convert them to uppercase) before the Equals method is invoked. Setting both characters to their uppercase equivalents removes any case-sensitivity between the character values, and they can be compared using the case-sensitive Equals comparison method as though it were a case-insensitive comparison.

You can further extend the overloaded IsCharEqual methods to handle the culture of the characters passed in to it:

public static bool IsCharEqual(char firstChar, CultureInfo firstCharCulture, 
                               char secondChar, CultureInfo secondCharCulture)
{
    return (IsCharEqual(firstChar, firstCharCulture, 
            secondChar, secondCharCulture, false));
}

public static bool IsCharEqual(char firstChar, CultureInfo firstCharCulture, 
                               char secondChar, CultureInfo secondCharCulture, 
                               bool caseSensitiveCompare)
{
    if (caseSensitiveCompare)
    {
        return (firstChar.Equals(secondChar));
    }
    else
    {
        return (char.ToUpper(firstChar, firstCharCulture).Equals
                    (char.ToUpper(secondChar, secondCharCulture)));
    }
}

The addition of the CultureInfo parameters to these methods allows us to pass in the culture information for the strings that we are calling ToUpper on. This information allows the ToUpper method to correctly uppercase the character based in the culture-specific details of the character (i.e., the language, region, etc., of the character).

Note that you must include the following using directives to compile this code:

using System;
using System.Globalization;
    [ Team LiB ] Previous Section Next Section