C#忽略字符串大小写
C#比较两字符串是否相等,忽略大小写
1.用String.Compare(s1, s2,true) == 0 忽略大小写
1 string s1 = "aaa";
2 string s2 = "Aaa";
3 //判断字符串值是否相等,忽略大小写
4 if (String.Compare(s1, s2,true) == 0)
5 {
6 //相等
7 }
8 else
9 {
10 //不等于
11 }
2.使用Equals方法添加一个判断条件“StringComparison.CurrentCultureIgnoreCase”
在比较两个字符串时,常常需要忽略大小写,此时有两种方法:
1、将已知的两个字符串都转化为大写,或者都转化为小写;
string str1 = "abcde_1";
string str2 = "ABcde_1";
Debug.Log("str1=str2 :" (str1.ToLower() == str2.ToLower()));
2、使用Equals方法添加一个判断条件“StringComparison.CurrentCultureIgnoreCase”
string str1 = "abcde_1";
string str2 = "ABcde_1";
Debug.Log("str1=str2 :" (String.Equals(str1, str2, StringComparison.CurrentCultureIgnoreCase)));
3、判断某个字符串是否包含特定字符,并忽略大小写(可使用indexof来判断)
string str1 = "abcde_1";
string str2 = "BcDe";
Debug.Log("str1是否包含str2:" (str1.Contains(str2)));
Debug.Log("str1是否包含str2:" (str1.IndexOf(str2,StringComparison.CurrentCultureIgnoreCase)>=0));
输出结果显示Contains方法无法忽略大小写,但可以使用先把字符串统一大小写然后再判断;
//compare string and ignore case
Console.WriteLine("Input new string 1: ");
string input1 = Console.ReadLine();
Console.WriteLine("Input new string 2: ");
string input2 = Console.ReadLine();
if(input1.Equals(input2, StringComparison.OrdinalIgnoreCase))
{
Console.WriteLine("Ignore case, successful!");
Console.WriteLine("True");
}
else
{
Console.WriteLine("False");
}
StringComparison.OrdinalIgnoreCase用于忽略大小写
Console.WriteLine("Input new string 1: ");
string input1 = Console.ReadLine();
Console.WriteLine("Input new string 2: ");
string input2 = Console.ReadLine();
input1 = input1.ToLower(); //Transform lower case
input2 = input2.ToLower();
if(input1.Equals(input2))
{
Console.WriteLine("Ignore case, successful!");
Console.WriteLine("True");
}
else
{
Console.WriteLine("False");
}
ToLower用于将字符串或者字符全部或者部分转换成小写
Console.WriteLine("Input new string 1: ");
string input1 = Console.ReadLine();
Console.WriteLine("Input new string 2: ");
string input2 = Console.ReadLine();
input1 = input1.ToUpper(); //Transform uppercase
input2 = input2.ToUpper();
if (input1.Equals(input2))
{
Console.WriteLine("Ignore case, successful!");
Console.WriteLine("True");
}
else
{
Console.WriteLine("False");
}
ToUpper用于将字符串或者字符全部或者部分转换成大写
这篇好文章是转载于:学新通技术网
- 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
- 本站站名: 学新通技术网
- 本文地址: /boutique/detail/tanhgheicb
-
photoshop保存的图片太大微信发不了怎么办
PHP中文网 06-15 -
《学习通》视频自动暂停处理方法
HelloWorld317 07-05 -
word里面弄一个表格后上面的标题会跑到下面怎么办
PHP中文网 06-20 -
Android 11 保存文件到外部存储,并分享文件
Luke 10-12 -
photoshop扩展功能面板显示灰色怎么办
PHP中文网 06-14 -
微信公众号没有声音提示怎么办
PHP中文网 03-31 -
excel下划线不显示怎么办
PHP中文网 06-23 -
excel打印预览压线压字怎么办
PHP中文网 06-22 -
TikTok加速器哪个好免费的TK加速器推荐
TK小达人 10-01 -
怎样阻止微信小程序自动打开
PHP中文网 06-13