//NSMutableString是NSString的子类
//可变字符串:NSMutableString,对可变字符串做增删改操作,就会对可变字符串直接进行操作
NSMutableString *mStr1 = [NSMutableString stringWithFormat:@"hello"];
//1.*****拼接字符串****
[mStr1 appendFormat:@" world"];
NSLog(@"mStr1 = %@",mStr1);
//2.*****删除********
[mStr1 deleteCharactersInRange:[mStr1 rangeOfString:@" world"]];
NSLog(@"mStr1 = %@",mStr1);
//3.*****插入********
[mStr1 insertString:@" girl" atIndex:5];
NSLog(@"mStr1 = %@",mStr1);
//可变字符串中的初始化方式(给定)
NSMutableString *mStr2 = [NSMutableString stringWithCapacity:100];
// mStr2 = @"dfsdf";可变字符串不能直接赋值(直接赋值的字符在常量区)
[mStr2 appendFormat:@"你真帅啊"];
[mStr2 insertString:@"帅哥" atIndex:0];
[mStr2 appendFormat:@"约吗?"];
NSLog(@"mStr2=%@",mStr2);
//4.*****更改********
NSRange range3 = [mStr2 rangeOfString:@"约吗?"];
[mStr2 replaceCharactersInRange:range3 withString:@"不约"];
NSLog(@"mStr2=%@",mStr2);
//5.*****重置********
[mStr2 setString:@"美女,你好漂亮"];
NSLog(@"mStr1 = %@",mStr2);
网友评论