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;
我不知道为什么CCC
是false
。有什么不对的吗?如果我设置culture = id, ko, en
等...然后CCC = true
。谁能帮我?
答
这是gI
这不在越南语的情况下等于GI
。 gi
(GI
)是音节初始,种类单个字母而gI
是两个单独的字母。其他对是
cH != CH
kH != KH
nG != NG
nH != NH
pH != PH
qU != QU
tH != TH
tR != TR
答
您可以尝试
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
将返回true
但CCC1
将返回false
因为文化。根据您的文化GID
和gId
是不同的。
这引发了一个例外:'文化'vi'是一种中立的文化。它不能用于格式化和解析,因此不能被设置为线程的当前文化.'您需要使用'vi-VN'(尽管它仍会返回false) –
对于非GUI应用程序,请使用StringComparer Comparer = StringComparer.Create(new CultureInfo(“vi-VN”),true); Comparer.Equals(“CategId”,“CATEGID”);'这是因为现在的文化很慢。 – Ben
由于你比较的字符串是“清晰”的(对于某些“清晰”值),你应该像Kevin所说的那样使用InvariantCultureIgnoreCase。当使用土耳其语的人触发[Dotted and dotless I](https://en.wikipedia.org/wiki/Dotted_and_dotless_I)问题时,我们遇到了与“设置名称”类似的问题,即“SID”不再等于“sid”忽视案件。 – TripeHound