• 字符串的插入、删除、查找并删除、重新赋值、替换


    #import <Foundation/Foundation.h>

    int main(int argc, const char * argv[]) {

        @autoreleasepool {

            

            //nsstring父类   NSMutableString可变字符串子类

            

    NSMutableString *mustr=[[NSMutableString alloc]init];

    NSMutableString *mustr1=[NSMutableString stringWithFormat:@"Hello"];

      //将不可变的字符串放置到可变字符串内

          NSString *str=@"welcome to oc";

           

            mustr=[NSMutableString stringWithString:str];

      //插入

            [mustr insertString:@" students " atIndex:7];

            [mustr appendString:@" teacher"];

            [mustr appendFormat:@"第二遍插入:%@",str];

      //删除

            [mustr deleteCharactersInRange:NSMakeRange(8, 10)];

      //查找并删除

            

            NSRange rang=[mustr rangeOfString:@"第二遍插入:welcome to oc"];

            if (rang.location!=NSNotFound) {

                [mustr deleteCharactersInRange:rang];

            }

         //重新赋值

            [mustr setString:str];

          //替换

            NSRange rang1=[mustr rangeOfString:@"oc"];

            [mustr replaceCharactersInRange:rang1 withString:@"IOS"];

            NSLog(@"%@",mustr);

        }

        return 0;

    }

  • 相关阅读:
    论架构在嵌软设计中的重要性
    妙用typeof关键字
    说说动态内存分配
    3个实用shell脚本,建议收藏!
    GNU C语法扩展(7)
    Ubunt_配置_nfs(文件挂载)
    Ubunt_配置_samba(文件共享)
    Ubunt_配置_tftp(文件传输)
    Ubunt_配置_net
    驱动_Platform平台总线
  • 原文地址:https://www.cnblogs.com/jidezhi/p/5120794.html
Copyright © 2020-2023  润新知