String.Compare 和 CultureInfo 的 CompareInfo 之间的区别
我正在尝试在一个小型桌面应用程序中移交 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我相信
String.Compare
将简单地委托给它提供的CompareInfo
- 只是更方便,而不必自己获取CompareInfo
。鉴于他们做同样的事情,我不确定你的最后一个问题的真正含义。
I believe
String.Compare
will simply delegate to theCompareInfo
it's provided - it's just more convenient not to have to get hold of theCompareInfo
yourself.Given that they do the same thing, I'm not sure what your final question really means.
是的,Jon skeet 是对的,String.Compare 内部调用了 CultureInfo.Compare 方法,这是来自 IL 的实际代码:
这里要注意的另一件事是,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:
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.