NSMutableString

// 创建NSMutableString
//        新语法只使用于NSString
        // 错误
//        NSMutableString *mStr1 = @"123"
        NSMutableString *mStr2 = [[NSMutableString alloc] init];
        NSMutableString *mStr3 = [[NSMutableString alloc] initWithCapacity:1];
        NSMutableString *mStr4 = [NSMutableString stringWithCapacity:1];
        mStr2.string = @"123";
        mStr3.string = @"123";
        mStr4.string = @"123";

        NSLog(@"mStr2:%@",mStr2);
        NSLog(@"mStr3:%@",mStr3);
        NSLog(@"mStr4:%@",mStr4);
        
        // 把NSString转换成NSMutableString
        NSString *str = @"abc";
        NSMutableString *mStr5 = [[NSMutableString alloc] initWithFormat:@"%@", str];
        NSMutableString *mStr6 = [[NSMutableString alloc] initWithFormat:@"wxhl"];

        NSLog(@"mStr5:%@",mStr5);
        NSLog(@"mStr6:%@",mStr6);
        
        // 追加字符串
        NSMutableString *mStr7 = [[NSMutableString alloc] init];
        [mStr7 appendString:@"wxhl"];
        NSLog(@"mStr7:%@", mStr7);
        [mStr7 appendString:@"@qq"];
        NSLog(@"mStr7:%@", mStr7);
        [mStr7 appendFormat:@"%d", 1234567];
        NSLog(@"mStr7:%@", mStr7);

        // 插入字符串
        [mStr7 insertString:@"0" atIndex:7];
        NSLog(@"mStr7:%@", mStr7);

        
        // 删除字符串
        NSRange range1 = {7, 8};
        [mStr7 deleteCharactersInRange:range1];
        NSLog(@"mStr7:%@", mStr7);

        // 替换
        NSRange range2 = {5, 2};
        [mStr7 replaceCharactersInRange:range2 withString:@"edu"];
        NSLog(@"mStr7:%@", mStr7);

你可能感兴趣的:(NSMutableString)