2.3比较两个字符时控制大小写敏感性

知识点:

1。.Equals 会默认完成区分大小写的比较。

使用Equals方法时,结合使用String类的ToUpper方法,就可以选择在比较串时是否考虑串的大小写。要对连个char变量完成区分大小写的比较,只需使用Equals方法,它会默认地完成区分大小写的比较。要完成一个不区分大小写的比较,需要在调用Equals方法之前先将两个字符转换为其相应的大写字符。

问题:

需要比较连个字符是否相等,但是需要有相应的灵活性,可以执行区分大小写的和不区分大小写的。

解决方案

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace _03比较两个字符时控制大小写敏感
 8 {
 9     class Program
10     {
11         static void Main(string[] args)
12         {
13            bool equal= IsCharEqual('a','A');
14            Console.WriteLine(equal);
15 
16         }
17         public static bool IsCharEqual(char firstChar, char secondChar) 
18         {
19             return IsCharEqual(firstChar, secondChar, false);
20         }
21 
22         private static bool IsCharEqual(char firstChar, char secondChar, bool caseSensitiveCompare)
23         {
24             if (caseSensitiveCompare)
25             {
26                 return (firstChar.Equals(secondChar));
27             }
28             else
29             {
30                 return (char.ToUpper(firstChar).Equals(char.ToUpper(secondChar)));
31             }
32         }
33     }
34 }
View Code
原文地址:https://www.cnblogs.com/weijieAndy/p/3995399.html