NSOrderedSame NSOrderedAscending NSOrderedDescending字符串枚举比较

本文介绍了如何使用Objective-C中的NSComparisonResult来比较字符串,包括字母比较、数字比较及大小写不敏感比较的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

字符串的比较,用 a compare:b 字母比较 越靠后面 越大

    NSString *a = @"qweqwe";  
    NSString *b = @"qweasd";  
    BOOL result = [a compare:b];  
    if (result == NSOrderedSame) {          // NSOrderedSame = 0 完全一样  
        NSLog(@"a = b");  
    }else if(result == NSOrderedAscending)  // NSOrderedAscending = -1  
        NSLog(@"a < b");  
    else{                                   //NSOrderedDescending = +1  
        NSLog(@"a > b");  
    }  

比较数字或者符号,或者字母 什么都行

   NSString *a = @"1.0.30qweqwe";  
    NSString *b = @"1.0.45qweasd";  
    BOOL result = [a compare:b];  
    if (result == NSOrderedSame) {          // NSOrderedSame = 0 完全一样  
        NSLog(@"a = b");  
    }else if(result == NSOrderedAscending)  // NSOrderedAscending = -1  
        NSLog(@"a < b");  
    else{                                   //NSOrderedDescending = +1  
        NSLog(@"a > b");  
    }  

不考虑大小写比较字符串

[a caseInsensitiveCompare:b]  

- (void)viewDidLoad  
{  
    NSString *a = @"i love my boyfriend.";  
    NSString *b = @"I Love My Boyfriend.";  
    NSLog(@" \n a:  %@ \n",a);  
    NSLog(@" \n b:  %@ \n",a);  
    BOOL result = [a caseInsensitiveCompare:b] ==  NSOrderedSame;  /* result = YES  
   */

不考虑大小写比较字符串

[a caseInsensitiveCompare:b]  

- (void)viewDidLoad  
{  
    NSString *a = @"i love my boyfriend.";  
    NSString *b = @"Little baby.";  
    NSLog(@" \n a:  %@ \n",a);  
    NSLog(@" \n b:  %@ \n",a);  
    BOOL result = [a caseInsensitiveCompare:b] ==  NSOrderedAscending;  /*  result = YES */
}  

有选择的比较大小 [a compare:b options:NSCaseInsensitiveSearch|NSNumericSearch]

- (void)viewDidLoad  
{  
    NSString *a = @"i love my boyfriend.";  
    NSString *b = @"I Love My Boyfriend.";  
    NSLog(@" \n a:  %@ \n",a);  
    NSLog(@" \n b:  %@ \n",b);  
    BOOL result = [a compare:b options:NSCaseInsensitiveSearch|NSNumericSearch] ==  NSOrderedSame; /* result = YES */ 
}  
NSCaseInsensitiveSearch  //忽略大小写的比较字符串  
NSNumericSearch       //比较字符串的个数  
NSLiteralSearch       //区分大小写,进行完全比较  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值