[OC Foundation框架 - 6] NSMutableString
NSString是不可变的
NSMutableString是可变字符串
1.初始化
2.串联字符串
3.替换字符串
4.插入字符串
5.删除字符串
1 #pragma mark buffer string create 2 void stringCreate() 3 { 4 NSMutableString *mutStr = [[NSMutableString alloc] initWithCapacity:10]; 5 [mutStr setString:@"Go!"]; 6 NSLog(@"%@", mutStr); 7 [mutStr appendString:@"Hello"]; 8 [mutStr appendFormat:@",age is %i and height is %.2f", 12, 1.54]; 9 NSLog(@"%@", mutStr); 10 11 //Replace string 12 NSRange r = [mutStr rangeOfString:@"age"]; 13 [mutStr replaceCharactersInRange:r withString:@"number"]; 14 NSLog(@"%@", mutStr); 15 16 //Insert string 17 [mutStr insertString:@"insert" atIndex:2]; 18 NSLog(@"%@", mutStr); 19 20 //Delete string 21 NSRange r2 = [mutStr rangeOfString:@"height"]; 22 [mutStr deleteCharactersInRange: r2]; 23 NSLog(@"%@", mutStr); 24 }