NSMutableString练习

  • 从要求讲3个520it拼接在一起

 

  • 会生成很多新的字符串

    NSString *res = @"";
    NSString *subStr = @"520";
    // 1.拼接字符串

//    res = [res stringByAppendingString:subStr];
//    res = [res stringByAppendingString:@" "];
//
//    res = [res stringByAppendingString:subStr];
//    res = [res stringByAppendingString:@" "];
//
//    res = [res stringByAppendingString:subStr];
//    res = [res stringByAppendingString:@" "];

    for (int i = 0; i < 3; ++i) {
        res = [res stringByAppendingString:subStr];
        res = [res stringByAppendingString:@" "];
    }

    // 2.删除末尾的空格
//    res = [res stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
    res = [res substringToIndex:res.length - 1];

    NSLog(@"res = |%@|", res);
  • 不会生成新的字符串
    NSString *subStr = @"520it";
    NSMutableString *res = [NSMutableString string];
    // 1.拼接字符串
    for (int i = 0; i < 3; ++i) {
        [res appendString:subStr];
        [res appendString:@" "];
    }
    // 2.删除空格
//    [res replaceCharactersInRange:NSMakeRange(res.length - 1, 1) withString:@""];
    [res deleteCharactersInRange:NSMakeRange(res.length - 1, 1)];
    NSLog(@"res = |%@|", res);

 

posted @ 2017-03-26 22:54  iFat  阅读(190)  评论(0编辑  收藏  举报