码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2014-09-26 21:59:38      阅读:124      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   io   os   使用   ar   sp   

知识点:

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

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

 

问题:

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

 

解决方案

bubuko.com,布布扣
 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

 

 

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

标签:style   blog   http   color   io   os   使用   ar   sp   

原文地址:http://www.cnblogs.com/weijieAndy/p/3995399.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!