#import <Foundation/Foundation.h> int main(int argc, const char * argv[]) { @autoreleasepool { /* 第1题 字符串中的world替换为i bookan wisdom */ NSLog(@"第一题"); NSString *str1=@"hello world and sunshine"; NSMutableString *newstr1=[NSMutableString stringWithString:str1]; NSRange range=[newstr1 rangeOfString:@"world"]; [newstr1 replaceCharactersInRange:range withString:@"i bookan wisdom"]; NSLog(@"%@",newstr1); /* 第2题 字符串的相加,以字符串输出,并输出长度 */ NSLog(@"第二题"); NSString *str2=@"999"; NSString *str3=@"888"; NSInteger s1=[str2 intValue]; NSInteger s2=[str3 intValue]; long su=s1+s2; NSString *sum=[[NSString alloc]initWithFormat:@"%ld",su]; NSLog(@"%@",sum); /* 第3题 比较字符串大小 */ NSLog(@"第三题"); NSString *str4=@"ABC"; NSString *str5=@"ABc"; NSComparisonResult result=[str4 compare:str5]; if (result==NSOrderedAscending) { NSLog(@"str4>str5"); }else if (result==NSOrderedDescending) { NSLog(@"str4<str5"); } else{ NSLog(@"str4=str5"); } /* 第4题 截取Android并输出 */ NSLog(@"第四题"); NSString *str6=@"iphoneAndroid"; NSMutableString *newstr6=[NSMutableString stringWithString:str6]; NSRange range1; range1=[str6 rangeOfString:@"Android"]; [newstr6 replaceCharactersInRange:range1 withString:@""]; NSLog(@"%@",newstr6); /* 第5题 用c语言创建一个oc字符串,并输出。使用两种方法。 */ NSLog(@"第五题"); char *s="zhangsanfeng"; printf("%s",s); printf(" "); NSLog(@"第一种方法"); NSString *news1=[NSString stringWithCString:s]; NSLog(@"%@",news1); NSLog(@"第二种方法"); NSString *news2=[NSString stringWithFormat:@"%s",s]; NSLog(@"%@",news2); /* 第6题 字符串内所有a都替换成A; */ NSLog(@"第六题"); NSString *str7=@"whatareyou"; NSMutableString *newstr7=[NSMutableString stringWithString:str7]; NSString *new=[newstr7 stringByReplacingOccurrencesOfString:@"a" withString:@"A"]; NSLog(@"%@",new); /* 第7题 判断字符串是否以http开头 */ NSLog(@"第七题"); NSString *str8=@"http://www.sina.com.cn"; BOOL result1=[str8 hasPrefix:@"http"]; if (result1!=NSOrderedSame) { NSLog(@"是"); } /* 第8题 将字符串内admin和123截取出来 */ NSLog(@"第八题"); NSString *str9=@"http://localhost:8099/test/user=admin&pwd=123"; NSRange range2; NSRange range3; NSRange newrange1; range2=[str9 rangeOfString:@"user="]; range3=[str9 rangeOfString:@"&pwd="]; newrange1.length=range3.location-(range2.location+range2.length); newrange1.location=(range2.location+range2.length); NSString *new1=[str9 substringWithRange:newrange1]; NSString *new2=[str9 substringFromIndex:range3.location+range3.length]; NSLog(@"%@",new1); NSLog(@"%@",new2); /* 第9题 字符添加telephone:iphone iphone5修改为"telephone:iphone iphone4 iphone5 iphone6"; */ NSLog(@"第九题"); NSString *str10=@"telephone:iphone iphone5"; NSMutableString *newstr10=[NSMutableString stringWithString:str10]; NSRange range4=[newstr10 rangeOfString:@"iphone5"]; [newstr10 deleteCharactersInRange:range4]; [newstr10 appendString:@"iphone4 iphone5 iphone6"]; NSLog(@"%@",newstr10); /* 第10题 排序 */ NSArray *arr=@[@"String 11",@"String 21",@"String 1",@"string 2",@"String 02"]; NSLog(@"第十题"); NSMutableArray *newarr=[NSMutableArray array]; for (int i=0; i<5;i++ ) { NSString *arrstr=[arr[i] substringFromIndex:7]; [newarr addObject:arrstr]; } [newarr sortUsingComparator:^NSComparisonResult(id obj1, id obj2) { int n1=[obj1 intValue]; int n2=[obj2 intValue]; if (n1>n2) { return NSOrderedAscending; } else if (n1<n2) { return NSOrderedDescending; } else { return NSOrderedSame; } }]; for (int i=0;i<newarr.count;i++) { NSMutableString *str=[NSMutableString string]; [str appendFormat:@"String %@",newarr[i]]; NSLog(@"%@",str); } } return 0; }