使用 '==
' 运算符和 string.Equals()
方法比较字符串是否存在任何全局化问题?如果是,避免这种情况的解决方案是什么?
最佳答案
通过 String.Equals
,您可以使用 StringComparison.InvariantCultureIgnoreCase
或 StringComparison.InvariantCulture
,使用 ==
你不能这样做。
您应该看到:The Turkish İ Problem and Why You Should Care
来自同一篇文章的代码片段:
using System;
using System.Globalization;
using System.Threading;
internal class Program
{
private static void Main(string[] args)
{
Thread.CurrentThread.CurrentCulture = new CultureInfo("tr-TR");
const string input = "interesting";
bool comparison = input.ToUpper() == "INTERESTING";
Console.WriteLine("These things are equal: " + comparison);
Console.ReadLine();
}
}
以上将返回一个 false
用于使用 ==
运算符的相等比较
你可以试试上面代码中的下面一行
bool Comparison2 = input.Equals("INTERESTING",
StringComparison.InvariantCultureIgnoreCase);
结果将是true
关于c# - 使用 '==' 运算符和 string.Equals() 方法比较字符串是否存在任何全局化问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13264057/