千锋独家出品《OC语言视频教程》

千锋独家出品《OC语言视频教程》

4 (2人评价)
  • 课时:(34)

  • 学员:(151)

  • 浏览:(11826)

  • 加入课程

千锋3G学院-OC语言-2.8-Category语法的笔记

相关课时: 笔记详情:

字符串取反

- (id)reverseString{

    // 取得字符串长度。self 表示当前字符串本身

    NSUInteger len = [self length];

    // 创建一个新的空字符串

    NSMutableString *retStr = [NSMutableString stringWithCapacity:len];    

    while (len > 0) {

        // 从后取一个字符 unicode, 两个字节

        unichar c = [self characterAtIndex:--len];       

        // 字符格式:当前c为双字节的国际字符,所以要用%C(大写)

        NSLog(@" c is %C", c);      

        // 取得该字符s

        NSString *s = [NSString stringWithFormat:@"%C", c];       

        // 单个字符s 加到字符串retStr 中

        [retStr appendString:s];

    }

    return retStr;

}

0 0