#pragma mark *** String comparison and equality ***
//这是字符串用来比较的方法
/* In the compare: methods, the range argument specifies the subrange, rather than the whole, of the receiver to use in the comparison. The range is not applied to the search string. For example, [@"AB" compare:@"ABC" options:0 range:NSMakeRange(0,1)] compares "A" to "ABC", not "A" to "A", and will return NSOrderedAscending.
*/
- (NSComparisonResult)compare:(NSString *)string;
- (NSComparisonResult)compare:(NSString *)string options:(NSStringCompareOptions)mask;
- (NSComparisonResult)compare:(NSString *)string options:(NSStringCompareOptions)mask range:(NSRange)compareRange;
- (NSComparisonResult)compare:(NSString *)string options:(NSStringCompareOptions)mask range:(NSRange)compareRange locale:(nullable id)locale; // locale arg used to be a dictionary pre-Leopard. We now accept NSLocale. Assumes the current locale if non-nil and non-NSLocale. nil continues to mean canonical compare, which doesn't depend on user's locale choice.
- (NSComparisonResult)caseInsensitiveCompare:(NSString *)string;//忽略大小写进行字符串比较
- (NSComparisonResult)localizedCompare:(NSString *)string;//本地化比较
- (NSComparisonResult)localizedCaseInsensitiveCompare:(NSString *)string;//本地化比较并且不区分大小写
- (NSComparisonResult)localizedStandardCompare:(NSString *)string;//它对应的选项是 NSCaseInsensitiveSearch 、 NSNumericSearch 、NSWidthInsensitiveSearch 以及 NSForcedOrderingSearch 。如果我们要在UI上显示一个文件列表,用它就最合适不过了。
返回值NSComparisonResult是一个结构体类型
typedef NS_ENUM(NSInteger, NSComparisonResult) {NSOrderedAscending = -1L, NSOrderedSame, NSOrderedDescending};
NSOrderedAscending升序,代表左边的值比右边的小;
NSOrderedSame左右两个值大小相同;
NSOrderedDescending降序,代表左边的值比右边的大。
比较方法:根据输入的options值决定比较顺序,依次比较各个字符的值的大小(a-z依次递增),第一个不一样的值决定比较结果大小
参数类型NSStringCompareOptions是一个结构体类型
typedef NS_OPTIONS(NSUInteger, NSStringCompareOptions) {
NSCaseInsensitiveSearch = 1, /* 不区分大小写比较 */
NSLiteralSearch = 2, /* 区分大小写*/
NSBackwardsSearch = 4, /* 从字符串尾端搜索*/
NSAnchoredSearch = 8, /* 搜索限制范围的字符串*/
NSNumericSearch = 64, /* 以字符串中的数字为标准比较*/
NSDiacriticInsensitiveSearch , /* 忽略“-”号*/
NSWidthInsensitiveSearch , /* 忽略字符串长度 */
NSForcedOrderingSearch , /* 忽略不区分大小写比较的选项,并强制返回 NSOrderedAscending 或者 NSOrderedDescending*/
NSRegularExpressionSearch NS_ENUM_AVAILABLE(10_7, 3_2) = 1024 /* Applies to rangeOfString:..., stringByReplacingOccurrencesOfString:..., and replaceOccurrencesOfString:... methods only; the search string is treated as an ICU-compatible regular expression; if set, no other options can apply except NSCaseInsensitiveSearch and NSAnchoredSearch */
};