String.Compare 和 CultureInfo 的 CompareInfo 之间的区别

发布于 2024-12-13 14:11:28 字数 926 浏览 0 评论 0原文

我正在尝试在一个小型桌面应用程序中移交 System.Gloablization。我对使用 String.Compare() 方法及其重载所提供的额外控制 CultureInfo.Compare() 感到有点困惑。

假设我有两个字符串

String s1 = "\u3057\u3093\u304B\u3093\u305b\u3093"; //some Japanese text in Unicode
String s2 = "\u30b7\u3043\u30ab\u30f3\u30bb\u30f3"; //Some Japanese text in Unicode
CultureInfo ci = new CultureInfo("ja-JP");

String.Compare 有几个重载,目前我将使用

String.Compare(String strA, String strB, CultureInfo Culture, CompareOptions options)

(其中 CompareOptions 是枚举类型)

但是,我也可以使用:

CompareInfo compareInfo = CompareInfo.GetCompareInfo("ja-JP");
compareInfo.Compare(String strA, String strB, CompareOptions options);

用于相同目的。

在处理全球化中的此类情况(例如日语文本)时,CultureInfo 的 CompareInfo 对象如何提供更多控制:CompareOptions.IgnoreKanaType(其中假名是日语的第二种形式,可以使用 CompareOptions 枚举类型从字符串中忽略)。

I was trying my hand over System.Gloablization in a small desktop app. I am a bit confused about the additional control CultureInfo.Compare() has to offer than using String.Compare() method and its overloads.

Let's say I have two strings

String s1 = "\u3057\u3093\u304B\u3093\u305b\u3093"; //some Japanese text in Unicode
String s2 = "\u30b7\u3043\u30ab\u30f3\u30bb\u30f3"; //Some Japanese text in Unicode
CultureInfo ci = new CultureInfo("ja-JP");

String.Compare has several overloads, out of which currently I would use

String.Compare(String strA, String strB, CultureInfo culture, CompareOptions options)

(where CompareOptions is an Enumerated type)

However, I could also use:

CompareInfo compareInfo = CompareInfo.GetCompareInfo("ja-JP");
compareInfo.Compare(String strA, String strB, CompareOptions options);

for the same purpose.

How does the CultureInfo's CompareInfo object provide more control when handling such situation in globalization such as Japanese text for eg: CompareOptions.IgnoreKanaType (where Kana is a second form of Japanese which can be ignored from the string using CompareOptions enumaerated type).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

嘿嘿嘿 2024-12-20 14:11:28

我相信 String.Compare 将简单地委托给它提供的 CompareInfo - 只是更方便,而不必自己获取 CompareInfo

鉴于他们做同样的事情,我不确定你的最后一个问题的真正含义。

I believe String.Compare will simply delegate to the CompareInfo it's provided - it's just more convenient not to have to get hold of the CompareInfo yourself.

Given that they do the same thing, I'm not sure what your final question really means.

乖乖哒 2024-12-20 14:11:28

是的,Jon skeet 是对的,String.Compare 内部调用了 CultureInfo.Compare 方法,这是来自 IL 的实际代码:

    public static int Compare(string strA, string strB, CultureInfo culture, CompareOptions options)
{
    if (culture == null)
    {
        throw new ArgumentNullException("culture");
    }
    return culture.CompareInfo.Compare(strA, strB, options);
}

这里要注意的另一件事是,CultureInfo.Compare 方法内部不会(首先)检查输入(culture ) 是否为空。它只是直接进行其他操作。如果使用 OrdinalIgnoreCase,它也会再次调用 string.Compare(string,string,StringOptions) API。
因此最好使用 String.Compare,因为在完成任何操作之前都会进行空检查。

Yes Jon skeet is right, String.Compare internally calls CultureInfo.Compare method, here is the actual code from IL:

    public static int Compare(string strA, string strB, CultureInfo culture, CompareOptions options)
{
    if (culture == null)
    {
        throw new ArgumentNullException("culture");
    }
    return culture.CompareInfo.Compare(strA, strB, options);
}

The other thing to be noticed here is that, CultureInfo.Compare method internally does not check (at first) for input (culture) is null or not. It just directly does other operation. Also it does calls again string.Compare(string,string,StringOptions) API if OrdinalIgnoreCase is used.
So best is to use String.Compare since there is a null checking before any operation is done.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文