1 #import <Foundation/Foundation.h> 2 3 int main(int argc, const char * argv[]) 4 { 5 @autoreleasepool 6 { 7 //可变字符串是字符串的子类 8 NSMutableString *str = [[NSMutableString alloc] initWithCapacity:1]; 9 [str setString:@"Hello"]; 10 11 //1.追加字符串 12 [str appendString:@" World"]; 13 NSLog(@"str = %@", str); 14 int a = 100; 15 [str appendFormat:@" a = %d", a]; 16 NSLog(@"str = %@", str); 17 18 //2.替换字符串 19 NSRange rang = [str rangeOfString:@"World"]; 20 [str replaceCharactersInRange:rang withString:@"IOS"]; 21 NSLog(@"str = %@", str); 22 23 //3.插入字符串 24 [str insertString:@"AAA" atIndex:6]; 25 NSLog(@"str = %@", str); 26 27 //4.删除字符串 28 NSRange rang1 = [str rangeOfString:@"AIOS"]; 29 [str deleteCharactersInRange:rang1]; 30 NSLog(@"str = %@", str); 31 } 32 return 0; 33 }
1 2016-08-10 14:58:17.314 NSMutableString[1215:182470] str = Hello World 2 2016-08-10 14:58:17.315 NSMutableString[1215:182470] str = Hello World a = 100 3 2016-08-10 14:58:17.315 NSMutableString[1215:182470] str = Hello IOS a = 100 4 2016-08-10 14:58:17.315 NSMutableString[1215:182470] str = Hello AAAIOS a = 100 5 2016-08-10 14:58:17.316 NSMutableString[1215:182470] str = Hello AA a = 100 6 Program ended with exit code: 0