zoukankan      html  css  js  c++  java
  • 可变字符串

    //

    //  main.m

    //  可变的字符串

    //

    //  Created by 博博 on 16/1/7.

    //  Copyright (c) 2016年 com.bb. All rights reserved.

    //

    #define NSLog(FORMAT, ...) printf("%s ", [[NSString stringWithFormat:FORMAT, ##__VA_ARGS__] UTF8String])

    #import <Foundation/Foundation.h>

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

        @autoreleasepool {

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

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

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

            NSLog(@"str=%@",mustr1);

            NSString *str=@"welcome to oc";

            mustr=[NSMutableString stringWithString:str];

            NSLog(@"%@",mustr);

            

            //插入

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

            NSLog(@"%@",mustr);

            [mustr appendString:@" teacher"];//插入末尾

            NSLog(@"%@",mustr);

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

            NSLog(@"%@",mustr);

            //删除

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

            NSLog(@"%@",mustr);

            //查找并删除

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

            if(rang.location!=NSNotFound){

                [mustr deleteCharactersInRange:rang];

            }

            NSLog(@"%@",mustr);

            //重新赋值

            [mustr setString:str];

            NSLog(@"%@",mustr);

            //替换

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

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

            NSLog(@"%@",mustr);

        }

        return 0;

    }

  • 相关阅读:
    Activity详解
    Log和LogCat的使用
    Android Studio项目目录结构
    Android系统架构
    [SCOI2016]美味
    [SCOI2016]背单词
    [SCOI2016]幸运数字
    [BZOJ4170]极光
    [JSOI2016]扭动的回文串
    [SCOI2016]萌萌哒
  • 原文地址:https://www.cnblogs.com/bobohahaha/p/5120913.html
Copyright © 2011-2022 走看看