String.Equals GID返回false?

问题描述:

我的ASP.NET MVC应用程序中有以下C#代码。我尝试使用Equals方法与culture = "vi"比较2 string。下面我的代码:String.Equals GID返回false?

string culture = "vi"; 

System.Threading.Thread.CurrentThread.CurrentCulture = new CultureInfo(culture); 
System.Threading.Thread.CurrentThread.CurrentUICulture = 
    System.Threading.Thread.CurrentThread.CurrentCulture; 

var CCC = string.Equals("CategId", "CATEGID", StringComparison.CurrentCultureIgnoreCase); 
var xx = string.Equals("TestGID", "TestGID", StringComparison.CurrentCultureIgnoreCase); 
var zz = string.Equals("id", "ID", StringComparison.CurrentCultureIgnoreCase); 

结果:

CCC = ;

xx = true;

zz = true;

我不知道为什么CCCfalse。有什么不对的吗?如果我设置culture = id, ko, en等...然后CCC = true。谁能帮我?

+0

这引发了一个例外:'文化'vi'是一种中立的文化。它不能用于格式化和解析,因此不能被设置为线程的当前文化.'您需要使用'vi-VN'(尽管它仍会返回false) –

+0

对于非GUI应用程序,请使用StringComparer Comparer = StringComparer.Create(new CultureInfo(“vi-VN”),true); Comparer.Equals(“CategId”,“CATEGID”);'这是因为现在的文化很慢。 – Ben

+0

由于你比较的字符串是“清晰”的(对于某些“清晰”值),你应该像Kevin所说的那样使用InvariantCultureIgnoreCase。当使用土耳其语的人触发[Dotted and dotless I](https://en.wikipedia.org/wiki/Dotted_and_dotless_I)问题时,我们遇到了与“设置名称”类似的问题,即“SID”不再等于“sid”忽视案件。 – TripeHound

这是gI越南语的情况下等于GIgiGI)是音节初始,种类单个字母gI两个单独的字母。其他对是

cH != CH 
kH != KH 
nG != NG 
nH != NH 
pH != PH 
qU != QU 
tH != TH 
tR != TR 

https://en.wikipedia.org/wiki/Vietnamese_language

您可以尝试

string culture = "vi"; 

System.Threading.Thread.CurrentThread.CurrentCulture = new CultureInfo(culture); 

System.Threading.Thread.CurrentThread.CurrentUICulture = 
    System.Threading.Thread.CurrentThread.CurrentCulture; 

var CCC = string.Equals("CategId", "CATEGID", StringComparison.InvariantCultureIgnoreCase); 
var CCC1 = string.Equals("CategId", "CATEGID", StringComparison.CurrentCultureIgnoreCase); 

在这个CCC将返回trueCCC1将返回false因为文化。根据您的文化GIDgId是不同的。