iOS字符串比较

在开发的过程中,经常遇到需要比较两个字符串,当然比较的方法很多,下面总结一下比较字符串的方法

一.用C语言比较:strcmp函数
char string1[] = "string!";
char string2[] = "string!";
if(strcmp(string1, string2) == 0)
{
    NSLog(@"1");
}

C中一般都是操作单个字符,不像OC有字符串对象的概念,C中需要操作一串字符,就需要用到数组.

二.用OC语言比较

1.isEqualToString方法:判断字符串是否相等

NSString *astring01 = @"This is a String!";
NSString *astring02 = @"This is a String!";
BOOL result = [astring01 isEqualToString:astring02];
NSLog(@"result:%d",result);

2.compare方法(comparer返回的三种值)

  • NSOrderedSame 判断两者内容是否相同
NSString *astring01 = @"This is a String!";
NSString *astring02 = @"This is a String!";    
BOOL result = [astring01 compare:astring02] == NSOrderedSame;    
NSLog(@"result:%d",result);    
  • NSOrderedAscending 判断两对象值的大小(按字母顺序进行比较,astring02大于astring01为真)
NSString *astring01 = @"This is a String!";
NSString *astring02 = @"this is a String!";
BOOL result = [astring01 compare:astring02] == NSOrderedAscending;    
NSLog(@"result:%d",result);
  • NSOrderedDescending 判断两对象值的大小(按字母顺序进行比较,astring02小于astring01为真)
NSString *astring01 = @"this is a String!";
NSString *astring02 = @"This is a String!";
BOOL result = [astring01 compare:astring02] == NSOrderedDescending;    
NSLog(@"result:%d",result);

3.不考虑大小写比较字符串

  • NSOrderedDescending判断两对象值的大小(按字母顺序进行比较,astring02小于astring01为真
NSString *astring01 = @"this is a String!";
NSString *astring02 = @"This is a String!";
BOOL result = [astring01 caseInsensitiveCompare:astring02] == NSOrderedSame;    
NSLog(@"result:%d",result);     
  • NSCaseInsensitiveSearch:不区分大小写比较 NSLiteralSearch:进行完全比较,区分大小写 NSNumericSearch:比较字符串的字符个数,而不是字符值。
NSString *astring01 = @"this is a String!";
NSString *astring02 = @"This is a String!";
BOOL result = [astring01 compare:astring02
                            options:NSCaseInsensitiveSearch | NSNumericSearch] == NSOrderedSame;    
NSLog(@"result:%d",result);

三.用Swift语言比较

  1. "==":判断字符串是否相等
let astring01 = "this is a String!";
let astring02 = "This is a String!";
print(astring01 == astring02)
  1. compare方法(compare返回的三种值)
  • ComparisonResult.orderedSame 判断两者内容是否相同
let astring01 = "this is a String!";
let astring02 = "This is a String!";
print(astring01.compare(astring02) ==  ComparisonResult.orderedSame)
  • ComparisonResult.orderedAscending 判断两值的大小(按照字母顺序比较,后者大于前者为真)
let astring01 = "this is a String!";
        let astring02 = "This is a String!";
        print(astring01.compare(astring02) ==  ComparisonResult.orderedAscending)
  • ComparisonResult.orderedDescending 判断两值的大小(按照字母顺序比较,前者大于后者为真)
let astring01 = "this is a String!";
        let astring02 = "This is a String!";
        print(astring01.compare(astring02) ==  ComparisonResult.orderedDescending)
  • 调用 rawValue,根据 Int 类型的返回值判断三种情况
    1 : 表示后者大于前者
    0 :表示相等
    -1 :表示前者大于后者
let astring01 = "this is a String!";
let astring02 = "This is a String!";
print(astring01.compare(astring02).rawValue)
  1. 不考虑大小写比较字符串
  • caseInsensitiveCompare方法:相当于全部转化成大写,或者全部转化成小写后,进行比较
let astring01 = "this is a String!";
let astring02 = "This is a String!";
print(astring01.caseInsensitiveCompare(astring02).rawValue)
  • localizedCaseInsensitiveCompare方法:不考虑大小写,对字符串进行完全比较
let astring01 = "this is a String!";
let astring02 = "This is a String!?";
print(astring01.localizedCaseInsensitiveCompare(astring02).rawValue)

另外,非完全比较,可以自定义比较选择(options)和比较范围(range)

let astring01 = "this is a String!";
let astring02 = "This is a String!";
astring01.compare(astring02, options: , range: >, locale: )

你可能感兴趣的:(iOS字符串比较)