热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

忽略大小写并在C#中进行比较[重复]-IgnorecaseandcompareinC#[duplicate]

Thisquestionalreadyhasananswerhere:这个问题在这里已有答案:CaselesslycomparingstringsinC#

This question already has an answer here:

这个问题在这里已有答案:

  • Caselessly comparing strings in C# 6 answers
  • 无情地比较C#6答案中的字符串

How to convert the string to uppercase before performing a compare, or is it possible to compare the string by ignoring the case

如何在执行比较之前将字符串转换为大写,或者是否可以通过忽略大小写来比较字符串

 if (Convert.ToString(txt_SecAns.Text.Trim()).ToUpper() == 
     Convert.ToString(hidden_secans.Value).ToUpper())

10 个解决方案

#1


26  

use this:

var result = String.Compare("AA", "aa", StringComparison.OrdinalIgnoreCase);

String.Compare Method (String, String, Boolean)

String.Compare方法(String,String,Boolean)

#2


12  

Case-insensitive string comparison is done like this in C#:

不区分大小写的字符串比较在C#中完成:

string.Equals("stringa", "stringb", StringComparison.CurrentCultureIgnoreCase)

Watch out! this code is culture dependant; there are several other options available, see http://msdn.microsoft.com/en-us/library/system.stringcomparison.aspx.

小心!这段代码依赖于文化;还有其他几个选项,请参阅http://msdn.microsoft.com/en-us/library/system.stringcomparison.aspx。

#3


5  

Well, you can use String.Equals(String,StringComparison) method. Just pass it StringComparison.InvariantCultureIgnoreCase or StringComparison.CurrentCultureIgnoreCase depending on your objectives...

好吧,你可以使用String.Equals(String,StringComparison)方法。只需传递StringComparison.InvariantCultureIgnoreCase或StringComparison.CurrentCultureIgnoreCase,具体取决于您的目标......

#4


4  

From MSDN:

String.Compare Method (String, String, Boolean):

String.Compare方法(String,String,Boolean):

public static int Compare(
    string strA,
    string strB,
    bool ignoreCase
)

so in your case:

在你的情况下:

if( String.Compare(txt_SecAns.Text.Trim(), hidden_secans.Value, true) == 0) 

#5


3  

txt_SecAns.Trim().Compare(hidden_secans.Trim(), StringComparison.CurrentCultureIgnoreCase)

#6


3  

string.Compare(string1, string2, true) == 0 will compare if the two strings are equal ignoring case

string.Compare(string1,string2,true)== 0将比较两个字符串是否相等而忽略大小写

#7


3  

Use StringComparison.CurrentCultureIgnoreCase:

if (txt_SecAns.Text.Trim().Equals(hidden_secans.Value.ToString(), StringComparison.CurrentCultureIgnoreCase))

#8


3  

String.Compare(str1, str2, true);

#9


2  

Just like this:

像这样:

if (string.Compare(txt_SecAns.Text.Trim(), hidden_secans.Value.ToString(), true) == 0)
{
    // DoSomething
}

The third parameter true tells string.Compare to ignore case.

第三个参数true告诉string.Compare忽略大小写。

#10


1  

I would personaly compare with a proper culture like everyone here, but something hasn't been suggested :

我会像这里的每个人一样比较适当的文化,但是没有提出一些建议:

public bool CompareStrings(string stringA, string StringB)
{
    return stringA.ToLower() == stringB.ToLower();
}

推荐阅读
author-avatar
修竹
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有