2.2确定一个字符是否在指定范围内

 

知识点:

1。大小在两个字符之间

 

问题:

要确定一个char数据类型中的字符是否在一个范围内,如数字1-5之间,或介于字母A—M之间

 

解决方案

可以使用Char数据类型内置的比较支持。以下代码显示了如何使用这个内置的比较支持;

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace _2._2确定一个字符是否在指定范围内
 8 {
 9     class Program
10     {
11         static void Main(string[] args)
12         {
13 
14         }
15 
16         public static bool IsInRange(char testChar,char startOfRange,char endOfRange) 
17         {
18             if (testChar >= startOfRange && testChar >=endOfRange)
19             {
20                 //testChar 在范围内
21                 return (true);
22             }
23             else
24             {
25                 //testChar 不在范围内
26                 return (false);
27             }
28         }
29 
30         public static bool IsInRangeCaseInsensitive(char testChar, char startOfRange, char startEndRange) 
31         {
32             testChar = char.ToUpper(testChar);
33             startOfRange = char.ToUpper(startOfRange);
34             startEndRange = char.ToUpper(startEndRange);
35             if (testChar >= startOfRange && testChar <= startEndRange)
36             {
37                 
38                 return (true);
39             }
40             else
41             {
42                 return (false);
43             }
44         }
45     }
46 }
View Code

 

posted @ 2014-09-26 17:49  伟杰Andy  阅读(316)  评论(0编辑  收藏  举报