
This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
40
|
Chapter 2: Strings and Characters
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 you to choose whether to take into account the case of the strings when com-
paring them. To perform a case-sensitive comparison of two
char variables, simply
use the
Equals method, which, by default, performs a case-sensitive comparison. Per-
forming 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 you 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, ...