#import <Foundation/Foundation.h>
int main(int argc, const char * argv[]) { @autoreleasepool {
NSString *str1 = @"qwe"; NSString *str2 = @"qwe";
//比较两个字符串的内容是否一样 BOOL b = [str1 isEqualToString:str2]; NSLog(@"\n b = %d",b);
//注意:使用==比较的是两个字符串的地址
//比较两个字符串第一个不同字符的大小 NSComparisonResult result = [str1 compare:str2];
//比较的时候不区分大小写 NSComparisonResult result2 = [str1 compare:str2 options:NSCaseInsensitiveSearch];
//比较的时候不区分大小写,并且参考字符串个数,相同情况下字符多的大 NSComparisonResult result3 = [str1 compare:str2 options:NSCaseInsensitiveSearch|NSNumericSearch];
switch (result2) { case NSOrderedAscending: NSLog(@"\n str1 < str2"); break;
case NSOrderedDescending: NSLog(@"\n str1 > str2"); break;
case NSOrderedSame: NSLog(@"\n str1 == str2"); break;
default: break; } } return 0; }
|