分类: iOS平台
2015-04-03 10:21:42
可变字符串
#import
int main(int argc, const char * argv[]) {
@autoreleasepool {
//指定字符串容量,,预先分配10个字符,效率会高
NSMutableString *s = [[NSMutableString alloc] initWithCapacity:10];
[s setString:@"123"];
NSLog(@"%@",s);
//拼接字符串
//没有返回值,改变的就是s
[s appendString:@"456"];
NSLog(@"%@",s);
[s appendFormat:@"789%f",0.123];
NSLog(@"%@",s);
//替换字符串
//NSRange ns = NSMakeRange(4, 2);
NSRange ns = [s rangeOfString:@"567"];
[s replaceCharactersInRange:ns withString:@"no"];
NSLog(@"%@",s);
//插入字符串
[s insertString:@"xxx" atIndex:3];
NSLog(@"%@",s);
//删除字符串
NSRange ns1 = [s rangeOfString:@"89"];
[s deleteCharactersInRange:ns1];
NSLog(@"%@",s);
}
return 0;
}