美文网首页iOS Developer
IOS 字符串的创建字符 添加字符 删除字符 替换方法

IOS 字符串的创建字符 添加字符 删除字符 替换方法

作者: JakieZhang | 来源:发表于2016-12-03 09:19 被阅读80次

    //创建方法

    NSMutableString*str1 = [[NSMutableStringalloc]init];//空字符串,有意义的

    NSMutableString*str2 = [NSMutableStringstringWithCapacity:100];//预估值

    NSMutableString*str3 =@"abc";//str3将退化成不可变字符串

    NSMutableString*str4 = [NSMutableStringstringWithString:@"string"];//标准创建方法

    NSMutableString*str5 = [NSMutableStringstringWithFormat:@"age:%d",6];//格式化创建方法

    //添加字符方法

    [str2appendString:@"The string"];//标准添加

    NSLog(@"%@", str2);

    [str2appendString:@" has 6 characters."];

    NSLog(@"%@", str2);

    NSMutableString*str6 = [NSMutableStringstringWithString:@"string"];

    [str6appendFormat:@" has %lu characters", str6.length];//格式化添加

    NSLog(@"%@", str6);

    //删除字符方法

    NSMutableString*str7 = [NSMutableStringstringWithString:@"I am learning Objective-C language."];

    //[str7 deleteCharactersInRange:NSMakeRange(14, 10)];

    [str7deleteCharactersInRange:[str7rangeOfString:@"Objective-"]];

    NSLog(@"%@", str7);

    //替换方法

    NSMutableString*str8 = [NSMutableStringstringWithString:@"This is a string"];

    [str8replaceCharactersInRange:[str8rangeOfString:@"a"]withString:@"another"];

    NSLog(@"%@", str8);

    相关文章

      网友评论

        本文标题:IOS 字符串的创建字符 添加字符 删除字符 替换方法

        本文链接:https://www.haomeiwen.com/subject/cyxcmttx.html