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

时间:2022-01-06 01:01:20

//    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 = [NSPredicatepredicateWithFormat:@"self contains [cd] %@",string];

    NSArray *arrays =  [[NSArrayalloc] initWithArray:[arrayfilteredArrayUsingPredicate:predicate]];

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




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

   NSString *string =@"hello world!";

   NSString *string2 =@"Hello World";

   BOOL result = [stringcaseInsensitiveCompare:string2] == NSOrderedSame;

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

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

    

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

   NSString *string3 =@"you are my";

   NSString *string4 =@"you Are my";

   BOOL result2 = [string3compare:string4

                             options:NSCaseInsensitiveSearch |NSNumericSearch] ==NSOrderedSame;

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

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



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