判断字符串中某个字符最后出现的位置

1,从开始截取字符串到某字符最后出现的位置

NSString *str = @"ff:ffhfffg:ff";
    str = [str substringToIndex:[str rangeOfString:@":" options:NSBackwardsSearch].location];

NSStringCompareOptions 参数

typedefNS_OPTIONS(NSUInteger, NSStringCompareOptions) {
    NSCaseInsensitiveSearch = 1,    //不区分大小写比较
    NSLiteralSearch = 2,    //逐字节比较 区分大小写
    NSBackwardsSearch = 4,    //从字符串末尾开始搜索
    NSAnchoredSearch = 8,    //搜索限制范围的字符串
    NSNumericSearch = 64,    //按照字符串里的数字为依据,算出顺序。例如 Foo2.txt < Foo7.txt < Foo25.txt
    NSDiacriticInsensitiveSearchNS_ENUM_AVAILABLE(10_5, 2_0) = 128,//忽略 "-" 符号的比较
    NSWidthInsensitiveSearchNS_ENUM_AVAILABLE(10_5, 2_0) = 256,//忽略字符串的长度,比较出结果
    NSForcedOrderingSearchNS_ENUM_AVAILABLE(10_5, 2_0) = 512,//忽略不区分大小写比较的选项,并强制返回 NSOrderedAscending 或者 NSOrderedDescending
    NSRegularExpressionSearchNS_ENUM_AVAILABLE(10_7, 3_2) = 1024   //只能应用于 rangeOfString:..., stringByReplacingOccurrencesOfString:...和 replaceOccurrencesOfString:... 方法。使用通用兼容的比较方法,如果设置此项,可以去掉 NSCaseInsensitiveSearch 和 NSAnchoredSearch
};
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容