iOS compare 字符串比较

IOS compare 字符串比较

NSString 比较字符串,我介绍一些常用的方法:(转载的🌶)

NSString *value = @"1234567890";
比较的方法:
[value compare:(NSString *)];
[value compare:(NSString *) options:(NSStringCompareOptions)];
[value compare:(NSString *) options:(NSStringCompareOptions) range:(NSRange)];
传入的参数:
compare:(NSString *)
传入一个需要比较的字符串。
例如 [value compare:@"1234567890"],返回 NSOrderedSame。
 
options:(NSStringCompareOptions)
传入 NSStringCompareOptions 枚举的值
enum{
    NSCaseInsensitiveSearch = 1,//不区分大小写比较
    NSLiteralSearch = 2,//区分大小写比较
    NSBackwardsSearch = 4,//从字符串末尾开始搜索
    NSAnchoredSearch = 8,//搜索限制范围的字符串
    NSNumbericSearch = 64//按照字符串里的数字为依据,算出顺序。例如 Foo2.txt < Foo7.txt < Foo25.txt
//以下定义高于 mac os 10.5 或者高于 iphone 2.0 可用
    ,
    NSDiacriticInsensitiveSearch = 128,//忽略 "-" 符号的比较
    NSWidthInsensitiveSearch = 256,//忽略字符串的长度,比较出结果
    NSForcedOrderingSearch = 512//忽略不区分大小写比较的选项,并强制返回 NSOrderedAscending 或者 NSOrderedDescending
//以下定义高于 iphone 3.2 可用
    ,
    NSRegularExpressionSearch = 1024//只能应用于 rangeOfString:..., stringByReplacingOccurrencesOfString:...和 replaceOccurrencesOfString:... 方法。使用通用兼容的比较方法,如果设置此项,可以去掉 NSCaseInsensitiveSearch 和 NSAnchoredSearch
}
range:(NSRange)
比较字符串的范围
 结构变量:
location: 需要比较的字串起始位置(以0为起始)
length: 需要比较的字串长度
 
返回值:
typedef enum _NSComparisonResult {
     NSOrderedAscending = -1,    // < 升序
     NSOrderedSame,              // = 等于
     NSOrderedDescending   // > 降序
} NSComparisonResult;
例子:版本号比较
    NSString *num1 = @"5.2.0";
    NSString *num2 = @"5.3.0";
 
    if ([num1 compare:num2 options:NSNumericSearch] == NSOrderedDescending)
    {
        ULog(@"%@ is bigger",num1);
    }else
    {
        ULog(@"%@ is bigger",num2);
    }

解释:
NSOrderedDescending是降序,如果numb1>numb2用这个函数相比较那么就等于降序

应用实例

    // 获取当前版本号
    NSDictionary *infoDic = [[NSBundle mainBundle] infoDictionary];
    NSString *appVersion = [infoDic objectForKey:@"CFBundleShortVersionString"];
    if ([appVersion compare:serverVersion options:NSNumericSearch] == NSOrderedAscending) {
        UIAlertView *alert;
        alert = [[UIAlertView alloc] initWithTitle:@"有新版本,是否升级!"
                                           message:message
                                          delegate: self
                                 cancelButtonTitle:@"取消"
                                 otherButtonTitles: @"升级", nil];
        alert.tag = 20;
        [alert show];
    }else{
        NSLog(@"没有新版本");
    }
//上一次本地存储的版本号    
    NSString *lastVersion = [[NSUserDefaults standardUserDefaults] objectForKey:@"Version"];
// 获取当前版本号
    NSString *currentVersion = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleShortVersionString"];
    
    if (lastVersion == nil || [currentVersion compare:lastVersion options:NSNumericSearch] == NSOrderedDescending){
        NSArray *imageArray = @[@"guidePage_1.png",@"guidePage_2.png",@"guidePage_3.png"];
        GuidePageView * guidePageView = [[GuidePageView alloc]initWithFrame:self.window.bounds imageArray:imageArray];
        [tabBar.view addSubview: guidePageView];
        
        //第一次进入完成后 记录一下
        [[NSUserDefaults standardUserDefaults]setObject:[[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleShortVersionString"] forKey:@"Version"];
    }
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容