NSMutableString 可变字符串的使用:
#import "MutableString.h"
@implementation MutableString
@synthesize _mutableString;
-(void)showMutableString
{
_mutableString = [[NSMutableString alloc] initWithCapacity:0];
NSLog(@"NSMutableString 地址:%p",&_mutableString);
//设置 字符串
[_mutableString setString:@"123"];
NSLog(@"NSMutableString 地址:%p",&_mutableString);
//追加字符串
[_mutableString appendString:@"456"];
NSLog(@"NSMutableString 地址:%p",&_mutableString);
NSLog(@"增加之前:%@",_mutableString);//123456
//替换字符串 NSMakeRange(NSUInteger location,length) location:位置,length:所替换长度
NSRange range = NSMakeRange(1,2);
[_mutableString replaceCharactersInRange:range withString:@"增加"];
NSLog(@"增加之后:%@",_mutableString);//1增加456
//插入字符串
[_mutableString insertString:@"插入" atIndex:4];
NSLog(@"插入数据后:%@",_mutableString);//1增加4插入56
//删除指定字符 从下标 6开始删除,删除长度为1
NSRange deleteRange = NSMakeRange(6, 1);
[_mutableString deleteCharactersInRange:deleteRange];
NSLog(@"删除数据后:%@",_mutableString);//1增加4插入6
//追加字符串
[_mutableString appendFormat:@"追加字符串%@%@%@",@"A",@"B",@"C"];
NSLog(@"追加字符串:%@",_mutableString);//1增加4插入6追加字符串ABC
}
@end