NSString
字符串创建:
1.NSString *strr = @"0123456789";
2.NSString *str = [NSString stringWithString:strr];
3.NSString *fstr = [NSString stringWithFormat:@"age = %d",age];
字符串长度;
NSUInteger len = str.length;
字符串获取子字符串:
1.NSString *fromStr = [str substringFromIndex:4];
2.NSString *toStr = [str substringToIndex:4];
3.NSRange range =NSMakeRange(4, 2);
NSString *rangeStr = [str substringWithRange:range];
字符串比较:
1.if([@"abc" isEqualToString:@"abc"]) //返回YES NO
2.NSComparisonResult result =[@"abc" compare:@"bc"];
switch (result) {
case NSOrderedSame:
NSLog(@"same");
break;
case NSOrderedAscending:
NSLog(@"abc<bc");
break;
case NSOrderedDescending:
NSLog(@"abc>bc");
break;
default:
break;
}
字符串查找:
NSString *url = @"http://www.baidu.com";
1.if ([url hasPrefix:@"http"]) //前缀
2.if ([url hasSuffix:@"com"]) //后缀
3.//字符串位置
NSRange rr =[url rangeOfString:@"www"];
if (rr.length) {
NSLog(@"%ld %ld",rr.location,rr.length);
}else{
NSLog(@"not found");
}
字符串转换:
//字符串转为数字 只把开头的数字转过来
NSString * heightStr = @"166.5";
float height = [heightStr floatValue];
//数字转为字符串
NSString *formatStr = [NSString stringWithFormat:@"height = %f",height];
NSMutableString
预留内存空间:
NSMutableString *mstr = [[NSMutableString alloc]initWithCapacity:0];
NSMutableString *mstr = [NSMutableString stringWithCapacity:0];
追加:
[mstr appendString:@"小王子"];
int num = 1;
[mstr appendFormat:@"%d 小王子", num ];
插入:
[mstr insertString:@"《" atIndex:0];
[mstr insertString:@"》" atIndex:mstr.length];
改:
NSRange rm =[mstr rangeOfString:@"小王子"];
[mstr replaceCharactersInRange:rm withString:@"大王子"];
重置:
[mstr setString:@"大王子"];
删:
NSRange rmm =[mstr rangeOfString:@"大"];
[mstr deleteCharactersInRange:rmm];