问题描述
NSString
的 localizedCaseInsensitiveCompare:
和 localizedStandardCompare: code>方法?
What is the difference between NSString
's localizedCaseInsensitiveCompare:
and localizedStandardCompare:
methods?
我阅读了参考文献,但没有正确了解要使用哪一个。
I read the reference but did not get a proper idea of which one to use.
推荐答案
localizedCaseInsensitiveCompare:
等效于:
[aString compare:otherString
options:NSCaseInsensitiveSearch
range:NSMakeRange(0,aString.length)
locale:[NSLocale currentLocale]];
localizedStandardCompare:
基本上等同于: / p>
localizedStandardCompare:
is basically equivalent to:
[aString compare:otherString
options:NSCaseInsensitiveSearch | NSNumericSearch
range:NSMakeRange(0,aString.length)
locale:[NSLocale currentLocale]];
因此,主要区别是字符串中的数字如何比较。
So, the primary difference is in how numbers within strings are compared.
使用 localizedCaseInsensitiveCompare:
比较以下3个字符串将导致以下顺序:
Comparing the following 3 strings using localizedCaseInsensitiveCompare:
would result in the following order:
"Foo2.txt",
"Foo25.txt",
"Foo7.txt"
另一方面,使用 localizedStandardCompare:
比较它们会产生以下顺序:
On the other hand, comparing them using localizedStandardCompare:
would result in the following order:
"Foo2.txt",
"Foo7.txt",
"Foo25.txt"
当 localizedCaseInsensitiveCompare:
方法一直存在时, localizedStandardCompare:
最近才添加(OS X 10.6)。 Finder使用数字方法排序文件名,在添加 localizedStandardCompare:
之前,开发人员没有一种简单的方法来确保他们可以像Finder那样排序字符串。
While the localizedCaseInsensitiveCompare:
method has been around forever, the localizedStandardCompare:
was only added recently (OS X 10.6). The Finder sorts filenames using the numeric method, and prior to the addition of localizedStandardCompare:
, developers were without an easy way to assure they could sort strings like the Finder did.
当确定要使用哪个字符串时,如果要比较的字符串表示文件名,那么您应该肯定会使用 localizedStandardCompare:
。
When determining which one to use, if the strings you're comparing represent filenames, then you should definitely tend toward using localizedStandardCompare:
.
这篇关于用于本地化的字符串比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!