// // main.m // compare // // Created by user on 15/10/1. // Copyright (c) 2015年 . All rights reserved. //
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[]) { @autoreleasepool {
#pragma mark - compare1 //compare //默认区分大小写 NSString *str1 = @"abc"; NSString *str2 = @"bcd";
//NSComparisonResul用来接收compare的结果 NSComparisonResult result = [str1 compare:str2];
//结果有三种 switch (result) { //Ascending(升序)1比2小 case NSOrderedAscending: NSLog(@"升序"); break;
//Dsecending(降序)1比2大 case NSOrderedDescending: NSLog(@"降序"); break;
//Same相等 case NSOrderedSame: NSLog(@"相等"); break; default: break; }
#pragma mark - compare2
NSString * str3 = @"Abc"; //compare..options.. //比较时加入选项 //这里加的是不区分大小写 NSComparisonResult result2 = [str1 compare:str3 options:NSCaseInsensitiveSearch]; //options里有很多选项点击进去查看
switch (result2) { //Ascending(升序)1比2小 case NSOrderedAscending: NSLog(@"升序"); break;
//Dsecending(降序)1比2大 case NSOrderedDescending: NSLog(@"降序"); break;
//Same相等 case NSOrderedSame: NSLog(@"相等"); break; default: break; }
//'|'用来添加多个选项 NSComparisonResult result3 = [str1 compare:str3 options:NSCaseInsensitiveSearch|NSCaseInsensitivePredicateOption];
#pragma mark - iSequalToString //isEqualToString //判断两个字符串是否相等 //返回值是BOOL类型(YES,NO) //区分大小写 if ([str1 isEqualToString:str3]) { NSLog(@"相等"); }else{ NSLog(@"不相等"); }
} return 0; }
|