object-c 之 NSMutableString

1 以C字符串构建OC字符串  
        NSMutableString * str1 = [NSMutableString stringWithUTF8String:"hello world"];
        NSLog(@"str1 = %@",str1);
2 可变字符串构造字符串一般形式
        NSMutableString *str2 = [NSMutableString stringWithString:str1];
        NSLog(@"str2 = %@",str2);
        
        NSMutableString *str3 = [NSMutableString stringWithFormat:@"%d###%s",1,"asdf"];
        NSLog(@"str3 = %@",str3);
        
3 以可变容量申请创建字符串
        NSMutableString *str4 = [NSMutableString stringWithCapacity:20];
        //以内容容量大小申请创建可变字符串 以后改变
4 删除
        [str2 deleteCharactersInRange:NSMakeRange(2, 2)];
        //[对象 方法]   执行结束之后自动返回给对象
        NSLog(@"str2 = %@",str2);
5 追加
     追加字符串
        [str2 appendString:str1];
        NSLog(@"str2 = %@",str2);
    格式化追加
        [str2 appendFormat:@"%d###%.1f",1,1.5];
        NSLog(@"str2 = %@",str2);
6 插入
        [str1 insertString:@"插入" atIndex:0];
        NSLog(@"str1 = %@",str1);
7 重置字符串 修改
        
        [str2 setString:@"hello world"];
        NSLog(@"str2 = %@",str2);
8 字符串替换
        NSMutableString *str5 = [NSMutableString stringWithString:@"nanjing boweifeng nanjing"];
        //1 出现的字符串  2  使用替换的  3附加条件  4  范围
        [str5 replaceOccurrencesOfString:@"jing" withString:@"替换" options:NSLiteralSearch range:NSMakeRange(0, 10)];
        NSLog(@"str5 = %@",str5);
        //用字符串 替换某个范围
        [str5 replaceCharactersInRange:NSMakeRange(0, 3) withString:@"范围"];
        NSLog(@"str5 = %@",str5);

你可能感兴趣的:(object-c 之 NSMutableString)