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

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

 NSArray *array = [[NSArray alloc]initWithObjects:@"beijing",@"shanghai",@"guangzou",@"wuhan",nil];
   NSString *string = @"Ang";
   NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF CONTAINS %@",string];
    NSLog(@"%@",[array filteredArrayUsingPredicate:pred]);

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

   if([arrayString rangeOfString:scanerString].location !=NSNotFound)
    {
        [StringArray addObject:arrayString];
    }

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

    NSArray *array = [[NSArray alloc]initWithObjects:@"beijing",@"shanghai",@"guangzou",@"wuhan", nil];
    NSString *string = @"HA";
    // 谓词搜索
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"self contains [cd] %@",string];
    NSArray *arrays =  [[NSArray alloc] initWithArray:[array filteredArrayUsingPredicate: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:比较字符串的字符个数,而不是字符值。

你可能感兴趣的:(iOS字符串比较(区分与不区分大小写比较))