C# 字符串比较:CurrentCultureIgnoreCase 和 InvariantCultureIgnoreCase 之间的区别

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/417954/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 02:37:33  来源:igfitidea点击:

C# String comparisons: Difference between CurrentCultureIgnoreCase and InvariantCultureIgnoreCase

c#stringcomparison

提问by Dan Esparza

When doing a string comparison in C#, what is the difference between doing a

在 C# 中进行字符串比较时,与执行字符串比较有什么区别

string test = "testvalue";
test.Equals("TESTVALUE", StringComparison.CurrentCultureIgnoreCase);

and

string test = "testvalue";
test.Equals("TESTVALUE", StringComparison.InvariantCultureIgnoreCase);

... and is it important to include that extra parameter, anyway?

......无论如何,包含那个额外的参数很重要吗?

采纳答案by Michael Burr

Microsoft gives some decent guidance for when to use the InvariantCultureproperty:

微软为何时使用该InvariantCulture属性提供了一些不错的指导:

MSDN: CultureInfo.InvariantCulture Property

MSDN:CultureInfo.InvariantCulture 属性

... an application should use the invariant culture only for processes that require culture-independent results, such as formatting and parsing data that is persisted to a file. In other cases, it produces results that might be linguistically incorrect or culturally inappropriate.

Security Considerations

If a security decision will be made based on the result of a string comparison or case change, your application should use an ordinal comparison that ignores case instead of using InvariantCulture. [...]

String Operations

If your application needs to perform a culture-sensitive string operation that is not affected by the value of CurrentCulture, it should use a method that accepts a CultureInfo parameter. [...]

Persisting Data

The InvariantCulture property is useful for storing data that will not be displayed directly to users. Storing data in a culture-independent format guarantees a known format that does not change. When users from different cultures access the data, it can be formatted appropriately based on specific user. [...]

...应用程序应仅将不变区域性用于需要与区域性无关的结果的进程,例如格式化和解析持久化到文件的数据。在其他情况下,它产生的结果可能在语言上不正确或在文化上不合适。

安全注意事项

如果将根据字符串比较或大小写更改的结果做出安全决策,您的应用程序应使用忽略大小写的序数比较,而不是使用 InvariantCulture。[...]

字符串操作

如果您的应用程序需要执行不受 CurrentCulture 值影响的区分区域性的字符串操作,则应使用接受 CultureInfo 参数的方法。[...]

持久化数据

InvariantCulture 属性对于存储不会直接向用户显示的数据很有用。以与文化无关的格式存储数据可保证已知格式不会改变。当来自不同文化的用户访问数据时,可以根据特定用户对其进行适当的格式化。[...]

回答by Jon Skeet

The other posts have given good advice, but I thought it might be nice to show an example of where it definitely makes a difference:

其他帖子给出了很好的建议,但我认为展示一个绝对有影响的例子可能会很好:

using System;
using System.Globalization;
using System.Threading;

class Test
{
    static void Main()
    {
        CultureInfo turkish = CultureInfo.CreateSpecificCulture("tr");
        Thread.CurrentThread.CurrentCulture = turkish;                

        // In Turkey, "i" does odd things
        string lower = "i";
        string upper = "I";

        Console.WriteLine(lower.Equals(upper, 
            StringComparison.CurrentCultureIgnoreCase));
        Console.WriteLine(lower.Equals(upper, 
            StringComparison.InvariantCultureIgnoreCase));
    }
}

(There are no doubt many other cases - this was just the first one I thought of.)

(毫无疑问,还有许多其他情况——这只是我想到的第一个。)