zoukankan      html  css  js  c++  java
  • NSMutableString

    NSString 是不可变,不能删除或者添加字符。NSString 的子类NSMutableString称为可变字符串

    创建方法

    -(id)initWithCapacity:(NSUInteger)capacity

    +(id)stringWithCapacity:(NSUInteger)capacity

    capacity 只是最优质,字符串的大小并不限制于所提供的容量,设置了capacity,可以预分配一块内存来存储它,速度会快很多。

    当然也可以使用创建NSString的方法来创建NSMutableString,因为NSMutableString是NSString 的子集,NSString能用的方法,NSMutableString都能使用


     #pragma mark 可变字符串的创建

    void stringCreate(){

    // Prealloc space with 10

    NSMutableString *str=[[NSMutableString alloc] initWithCapacity:10];

    //set content for string

    [str setString:@"1234"];

    //append string

    [str appendString:@"567"];//void 无返回值

    [str appendFormat:@"age is %i and height is %.2f",27,1.55f];

    //replace

    [str replaceCharactersInRange:[str rangeOfString:@"age"]  withString:@"no"]

    //insert string index 2就是a  123

    [str insertString:@"abc" atIndex:2]

    //delete string 给了范围就删了

    [str deleteCharacterInRange:[str rangeOfString:@"age"]];

    NSLog(@"%@",str);

    [str release];

    }

  • 相关阅读:
    The Fifth Week Lucklyzpp
    The Fourth Week Lucklyzpp
    The Third Week Lucklyzpp
    The Second Week lucklyzpp
    快10年没怎么过来了,一切如常
    男女诗篇
    ubuntu安装mysql
    ubuntu配置tomcat和jdk
    ubuntu常用操作命令以及它的通道模式简解
    Ubuntu操作异常汇总
  • 原文地址:https://www.cnblogs.com/yesihoang/p/4512294.html
Copyright © 2011-2022 走看看