Object-c之可变字符串

/***********可变数组可以使用不可变数组的方法***********/

       

       //创建可变字符串对象

       NSMutableString *Str1 = [NSMutableString string];

       NSLog(@"Str1 = %@", Str1); // 空字符串

       

       //在指定位置增加字符串(空字符串只能在0位开始增加)

       [Str1 insertString:@"hello world" atIndex:0];

       NSLog(@"Str1 = %@", Str1);

       [Str1 insertString:@" 中国" atIndex:11];

       NSLog(@"Str1 = %@", Str1);

       // 输出: Str1 = hello world Str1 = hello world 中国

 

       // 删除指定范围内的字符

       [Str1 deleteCharactersInRange:NSMakeRange(5, 6)];

       NSLog(@"Str1 = %@", Str1);

       // 输出: Str1 = hello 中国

       

       //追加字符

       [Str1 appendString:@" 哈哈哈"];

       NSLog(@"Str1 = %@", Str1);

       // 输出: Str1 = hello 中国哈哈哈

 

       //格式化追加字符串

       [Str1 appendFormat:@"%s%d","welcome ", 38];

       NSLog(@"Str1 = %@",Str1);

       // 输出: Str1 = hello 中国哈哈哈welcome 38

       

       //修改字符串

       [Str1 setString:@"I love you"];

       NSLog(@"Str1 = %@",Str1);

       // 输出: Str1 = I love you

       

       //替换指定范围内的字符

       [Str1 replaceCharactersInRange:NSMakeRange(2,4) withString: @"fuck"];

       NSLog(@"Str1 = %@",Str1);

       // 输出: Str1 = I fuck you

       

       //用给定的字符串对象替换所有目的的字符串

       [Str1 replaceOccurrencesOfString:@"fuck" withString:@"want fuck" options:NSLiteralSearch range:NSMakeRange(1,8)];

       NSLog(@"Str1 = %@",Str1);

       // 输出: Str1 = I want fuck you

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值