iOS字符串比较(区分与不区分大小写比较)

时间:2020-12-12 00:53:28

//    1、比较字符串(搜索功能)(区分大小写)方法一:

    NSArray *array = [[NSArrayalloc]initWithObjects:@"beijing",@"shanghai",@"guangzou",@"wuhan",nil];

   NSString *string = @"Ang";

   NSPredicate *pred = [NSPredicatepredicateWithFormat:@"SELF CONTAINS %@",string];

    NSLog(@"%@",[arrayfilteredArrayUsingPredicate:pred]);

    

//    2、比较字符串(搜索功能)(区分大小写)方法二:

   if([arrayString rangeOfString:scanerString].location !=NSNotFound)

    {

        [StringArray addObject:arrayString];

    }


//    3、比较字符串(搜索功能)(不区分大小写,谓词搜索)方法三:

    NSArray *array = [[NSArrayalloc]initWithObjects:@"beijing",@"shanghai",@"guangzou",@"wuhan"nil];

    NSString *string = @"HA";

    // 谓词搜索

    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"self contains [cd] %@",string];

    NSArray *arrays =  [[NSArray allocinitWithArray:[arrayfilteredArrayUsingPredicate:predicate]];

    NSLog(@"=-- %@",arrays);




//    4、比较字符串(不区分大小写)方法一:

   NSString *string = @"hello world!";

   NSString *string2 = @"Hello World";

   BOOL result = [string caseInsensitiveCompare:string2] == NSOrderedSame;

   NSLog(@"result:%d",result);

    //NSOrderedDescending判断两对象值的大小(按字母顺序进行比较,astring02小于astring01)

    

//    5、比较字符串(不区分大小写)方法二:

   NSString *string3 = @"you are my";

   NSString *string4 = @"you Are my";

   BOOL result2 = [string3 compare:string4

                             options:NSCaseInsensitiveSearch |NSNumericSearch] ==NSOrderedSame;

   NSLog(@"result:%d",result2);

    //NSCaseInsensitiveSearch:不区分大小写比较 NSLiteralSearch:进行完全比较,区分大小写NSNumericSearch:比较字符串的字符个数,而不是字符值。



转载请注明出处:http://blog.csdn.net/sevenquan