我的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
。誰能幫我?
這引發了一個例外:'文化'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