NSString 两个字符串的比较,用 a compare:b 来比,得出的结果分3种
26个字母比较 越靠后面 越大
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”);
}
2011-07-05 15:04:33.951 Q[5180:207] a > b
2.比较数字或者符号,或者字母 什么都行
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”);
}
2011-07-05 15:05:13.175 Q[5209:207] a < b
3.不考虑大小写比较字符串
[a caseInsensitiveCompare:b]
4.不考虑大小写比较字符串
[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 = (BOOL) YES;
版权声明:本文为博主原创文章,未经博主允许不得转载。
NSString [a compare:b] NSOrderedSame NSOrderedAscending NSOrderedDescending字符串比较
原文地址:http://blog.csdn.net/yangchen9931/article/details/47999573