NSString进行比较的方法和属性

#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 */

};


location: 需要比较的字符串起始位置(以0为起始)





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值