Recipe2.2.Determining Whether a Character Is Within a Specified Range


Recipe 2.2. Determining Whether a Character Is Within a Specified Range

Problem

You need to determine whether a character in a char data type is within a range, such as between the numbers 1 and 5 or between the letters A and M.

Solution

Use the built-in comparison support for the char data type. The following code shows how to use the built-in comparison support:

 public static bool IsInRange(char testChar, char startOfRange, char endOfRange) {     if (testChar >= startOfRange && testChar <= endOfRange)     {         // testChar is within the range.         return (true);     }     else     {         // testChar is NOT within the range.         return (false);     } } 

There is only one problem with that code. If the startOfRange and endOfRange characters have different cases, the result may not be what you expect. The following code, which makes a case-insensitive comparison, can be used to solve this problem:

 public static bool IsInRangeCaseInsensitive(char testChar,                    char startOfRange, char endOfRange) {     testChar = char.ToUpper(testChar);     startOfRange = char.ToUpper(startOfRange);     endOfRange = char.ToUpper(endOfRange);     if (testChar >= startOfRange && testChar <= endOfRange)     {         // testChar is within the range.         return (true);     }     else     {         // testChar is NOT within the range.         return (false);     } } 

Discussion

The IsInRange method accepts three parameters. The first is the testChar character that you need to check on, to test if it falls between the last two parameters on this method. The last two parameters are the starting and ending characters, respectively, of a range of characters. The testChar parameter must be between startOfRange and endOfRange or equal to one of these parameters for this method to return TRue; otherwise, false is returned.

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) 



C# Cookbook
Secure Programming Cookbook for C and C++: Recipes for Cryptography, Authentication, Input Validation & More
ISBN: 0596003943
EAN: 2147483647
Year: 2004
Pages: 424

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net