#import <Foundation/Foundation.h>
int main(int argc, const char * argv[]) {
@autoreleasepool {
/*
第1题 字符串中的world替换为i bookan wisdom
*/
NSString *str1=@"hello world and sunshine";
//给可变字符串开辟空间
NSMutableString *mustr=[[NSMutableString alloc]init];
//将不可变字符串赋值给可变字符串
mustr=[NSMutableString stringWithString:str1];
//替换
NSRange rang=[mustr rangeOfString:@"world"];
[mustr replaceCharactersInRange:rang withString:@"i bookan wisdom"];
NSLog(@"%@",mustr);
/*
第2题 字符串的相加,以字符串输出,并输出长度
*/
NSString *str21=@"999";
NSString *str22=@"888";
NSString *string = [str21 stringByAppendingString:str22];
NSUInteger count=[string length];
NSLog(@"%@ %lu",string,count);
/*
第3题 比较字符串大小
*/
NSString *str4=@"ABC";
NSString *str5=@"ABc";
NSComparisonResult result=[str4 compare:str5];
if (result==NSOrderedSame) {
NSLog(@"str4=str5");
}else if (result==NSOrderedAscending){
NSLog(@"str4<str5");
}else if(result==NSOrderedDescending){
NSLog(@"str4>str5");
}
/*
第4题 截取Android并输出
*/
NSString *str6=@"iphoneAndroid";
NSLog(@"%@",[str6 substringWithRange:NSMakeRange(6, 7)]);
//第二种
NSRange range1=[str6 rangeOfString:@"Android"];
NSString *subStr=[str6 substringWithRange:range1];
NSLog(@"%@",subStr);
//第三种
NSString *substr1=[str6 substringFromIndex:range1.location];
NSLog(@"%@",substr1);
/*
第5题 用c语言创建一个oc字符串,并输出。使用两种方法。
*/
char *s="zhangsanfeng";
printf("%s",s);
NSString *new=[NSString stringWithFormat:@"%s",s];
NSLog(@"%@",new);
NSString *new2 = [[NSString alloc] initWithCString:s];
NSLog(@"%@",new2);
/*
第6题 字符串内所有a都替换成A;
*/
NSString *str7=@"whatareyou";
NSString * str7gai= [str7 stringByReplacingOccurrencesOfString:@"a" withString:@"A"];
NSLog(@"%@",str7gai);
/*
第7题 判断字符串是否以http开头
*/
NSString *str8=@"http://www.sina.com.cn";
NSString *str8s=[str8 substringToIndex:4];
NSString *str8ss=@"http";
NSComparisonResult str8duibi=[str8s compare:str8ss];
if (str8duibi==NSOrderedSame) {
NSLog(@"字符串是以http开头");
/*
第8题 将字符串内admin和123截取出来
*/
NSString *str9=@"http://localhost:8099/test/user=admin&pwd=123";
NSRange range91=[str9 rangeOfString:@"admin"];
NSString *subStr=[str9 substringWithRange:range91];
NSRange range92=[str9 rangeOfString:@"123"];
NSString *subStr2=[str9 substringWithRange:range92];
NSLog(@"%@,%@",subStr,subStr2);
/*
第9题 字符添加telephone:iphone iphone5修改为"telephone:iphone iphone4 iphone5 iphone6";
*/
NSString *str10=@"telephone:iphone iphone5";
NSRange rang10=[str10 rangeOfString:@"iphone5"];
NSMutableString *mustr10=[[NSMutableString alloc]init];
//将不可变字符串赋值给可变字符串
mustr10=[NSMutableString stringWithString:str10];
//替换
[mustr10 replaceCharactersInRange:rang10 withString:@"iphone4 iphone5 iphone6"];
NSLog(@"%@",mustr10);
/*
第10题 排序
*/
NSArray *arr=@[@"String 11",@"String 21",@"String 1",@"string 2",@"String 02"];
arr=[arr sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
NSComparisonResult result=[obj1 compare:obj2];
return result==NSOrderedAscending;
}];
NSLog(@"将数组中的元素进行排序");
for (NSString *str in arr) {
NSLog(@"%@",str);
}
}
}
return 0;
}