
This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
Controlling Case Sensitivity When Comparing Two Characters
|
39
The IsInRange method can be called in the following manner:
bool inRange = IsInRange('c', 'a', 'g');
bool inRange = IsInRange('c', 'a', 'b');
bool inRange = IsInRange((char)32, 'a', 'g');
The first call to this method returns true, since c is between a and g. The second
method returns
false, since c is not between a and b. The third method indicates
how an integer value representative of a character would be passed to this method.
Note that this method tests whether the
testChar value is inclusive between the
range of characters
startOfRange and endOfRange. If you wish to determine only
whether
testChar is between this range exclusive of the startOfRange and endOfRange
character values, you should modify the if statement, as follows:
if (testChar > startOfRange && testChar < endOfRange)
2.3 Controlling Case Sensitivity When Comparing
Two Characters
Problem
You need to compare two characters for equality, but you need the flexibility of per-
forming 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 ...