iOS基础学习

Objective-C基础学习之NSMutableString基

2017-10-23  本文已影响9人  WenJim

1.NSMutableString 基本概念

2.字符串中的可变和不可变

   // 改变了指针的指向, 并没有修改字符串
    NSString *str = @"cwj";
    str = @"cwjLove";

    // 生成了一个新的字符串, 并没有修改字符串
    NSString *newStr = [str substringFromIndex:1];
    NSLog(@"str = %@", str);
    NSLog(@"newStr = %@", newStr);
    NSMutableString *strM = [NSMutableString string];
    NSLog(@"strM = %@", strM);
     // 修改原有字符串, 没有生成新的字符串
    [strM appendString:@"cwj"];
    NSLog(@"strM = %@", strM);
    [strM appendString:@" v587"];
    NSLog(@"strM = %@", strM);

NSMutableString常用方法

1.NSMutableString常用方法

NSMutableString *strM = [NSMutableString string];
    NSLog(@"strM = %@", strM);
    [strM appendString:@"cwj"];
    NSLog(@"strM = %@", strM);
NSMutableString *strM = [NSMutableString string];
[strM appendFormat:@"/age is %i", 10];
    NSMutableString *strM = [NSMutableString stringWithString:@"http://www.xiaoningle.com"];
     // 一般情况下利用rangeOfString和deleteCharactersInRange配合删除指定内容
     NSRange range = [strM rangeOfString:@"http://"];
     [strM deleteCharactersInRange:range];
     NSLog(@"strM = %@", strM);
    NSMutableString *strM = [NSMutableString stringWithString:@"www.xiaoningle.com"];
    [strM insertString:@"http://" atIndex:0];
    NSLog(@"strM = %@", strM);
    NSMutableString *strM = [NSMutableString stringWithString:@"http://www.xiaoningle.com/logo.png"];
    NSRange range = [strM rangeOfString:@"cwj"];
    [strM replaceOccurrencesOfString:@"cwj" withString:@"cwjL" options:0 range:range];
    NSLog(@"strM = %@", strM);

2.字符串使用注意事项

NSMutalbeString *s1 = @”cwj”;
// 会报错
[strM insertString:@"my name is " atIndex:0];
上一篇下一篇

猜你喜欢

热点阅读