-
/*可变字符串,注意NSMutableString是NSString子类*/
-
//注意虽然initWithCapacity分配字符串大小,但是不是绝对的不可以超过此范围,声明此变量对 性能有好处
-
NSMutableString *str1= [[NSMutableString alloc] initWithCapacity:10];
-
[str1 setString:@"hello"];//设置字符串
-
NSLog(@"%@",str1);
-
//结果:hello
-
-
[str1 appendString:@",world!"];//追加字符串 NSLog(@"%@",str1);
-
//结果:hello,world
-
-
[str1 appendFormat:@"我的年龄是%i。dear,I love you.",18];
-
NSLog(@"%@",str1);
-
//结果:hello,world!我的年龄是18。dear,I love you.
-
-
//替换字符串
-
NSRange range=[str1 rangeOfString:@"dear"];
-
[str1 replaceCharactersInRange:range withString:@"Honey"];
-
NSLog(@"%@",str1);
-
//结果:hello,world!我的年龄是18。Honey,I love you.
-
-
//插入字符串
-
[str1 insertString:@"My name is Kenshin." atIndex:12];
-
NSLog(@"%@",str1);
-
//结果:hello,world!My name is Kenshin.我的年龄是18。Honey,I love you.
-
-
//删除指定字符串
-
//[str1 deleteCharactersInRange:NSMakeRange(5, 12)];
-
//删除str1字符串里面的从下标5开始(包含)长度12的子字符串
-
-
//NSRange n = [str1 rangeOfString:@"My name is Kenshin."];
-
//[str1 deleteCharactersInRange:n];
-
//找到指定子字符串再str1里面的起始位置和长度
-
//然后进行删除
-
-
[str1 deleteCharactersInRange:[str1 rangeOfString:@"My name is Kenshin."]];// 删除指定范围的字符串
-
NSLog(@"%@",str1); //结果:hello,world!我的年龄是18。Honey,I love you.
-