#import <Foundation/Foundation.h>
//NSString 创建不可变字符串对象
//字符串对象本身不可以被修改
//NSMutableString是NSString的子类
//创建可变字符串对象
//字符串对象本身可以被修改
//可变字符串对象可以直接调用不可变字符串的方法
int main(int argc, const char * argv[]) {
@autoreleasepool {
NSMutableString *str1 = [[NSMutableString alloc] initWithString:@"hello world"];
NSLog(@"str1 = %@", str1);
//创建指定容量大小的 空 可变字符串对象
//@"" 空字符串对象
NSMutableString *str2 = [[NSMutableString alloc] initWithCapacity:20];
NSLog(@"str2 = %@", str2);
//在指定位置增加字符串
//- (void)insertString:(NSString *)aString atIndex:(NSUInteger)loc;
[str1 insertString:@" welcome" atIndex:5];
NSLog(@"str1 = %@", str1);
//删除指定范围内的字符
//- (void)deleteCharactersInRange:(NSRange)range;
[str1 deleteCharactersInRange:NSMakeRange(6, 3)];
NSLog(@"str1 = %@", str1);
//在字符串末尾追加字符串
//- (void)appendString:(NSString *)aString;
[str1 appendString:@" baidu"];
NSLog(@"str1 = %@", str1);
//格式化追加字符串
//- (void)appendFormat:(NSString *)format, ...
[str1 appendFormat:@"%d%s", 123, ".com"];
NSLog(@"str1 = %@", str1);
//用传入的字符串对象重置字符串
//- (void)setString:(NSString *)aString;
[str1 setString:@"welcome to china"];
NSLog(@"str1 = %@", str1);
//用传入的字符串替换指定范围内的字符
//- (void)replaceCharactersInRange:(NSRange)range withString:(NSString *)aString;
[str1 replaceCharactersInRange:NSMakeRange(8, 2) withString:@"like"];
NSLog(@"str1 = %@", str1);
//用传入的字符串替换指定范围内的目标字符串
//- (NSUInteger)replaceOccurrencesOfString:(NSString *)target withString:(NSString *)replacement options:(NSStringCompareOptions)options range:(NSRange)searchRange;
//第一个参数:替换的目标参数
//第二个参数:传入的字符串
//第三个参数:字符串搜索模式
//第四个参数:字符串搜索范围
NSMutableString *str3 = [NSMutableString stringWithUTF8String:"hello world world chian world hello"];
[str3 replaceOccurrencesOfString:@"world" withString:@"baidu" options:NSLiteralSearch range:NSMakeRange(6, 17)];
NSLog(@"str3 = %@", str3);
}
return 0;
}