#import
int main(int argc, const char * argv[]) {
@autoreleasepool {
NSMutableString *mustr1=[[NSMutableString alloc]init];
NSMutableString *mustr2=[NSMutableString stringWithFormat:@"Hello"];
NSMutableString *mustr3=[[NSMutableString alloc]init];
NSString *str=@"welcom to OC";
//NSMutableString *str=@"welcom to OC"; 这是错误的——不可以将字符串常量直接赋值给NSMutableString
//赋值
mustr1=[NSMutableString stringWithString:str];
NSLog(@"mustr1=%@",mustr1);
//替换字符串
NSRange rang2=[mustr1 rangeOfString:@"OC"];
[mustr1 replaceCharactersInRange:rang2 withString:@"IOS"];
NSLog(@"mustr1=%@",mustr1);
//插入
[mustr1 insertString:@" student" atIndex:6]; //指定位置6插入字符串
[mustr2 appendString:@" teacher!"]; //末尾插入字符串
[mustr3 appendFormat:@"Hey!%@",str]; //格式化插入字符串
NSLog(@"mustr1=%@",mustr1);
NSLog(@"mustr2=%@",mustr2);
NSLog(@"mustr3=%@",mustr3);
//指定位置删除
[mustr1 deleteCharactersInRange:NSMakeRange(7, 8)]; //在位置7删除8个字符
NSLog(@"mustr1=%@",mustr1);
//查找删除
NSRange rang1=[mustr2 rangeOfString:@"teacher"];
if(rang1.location!=NSNotFound) {
[mustr2 deleteCharactersInRange:rang1];
}
NSLog(@"mustr2=%@",mustr2);
//重新赋值
[mustr2 setString:str];
NSLog(@"mustr2=%@",mustr2);
}
return 0;
}