标签:
方法一:
NSString *str = @"abcdefg";
NSMutableString *reverseString = [NSMutableString string];
for (int i = str.length - 1; i >= 0; i--) {
char c = [str characterAtIndex:i];
[reverseString appendFormat:@"%c", c];
}
NSLog(@"%@", reverseString);
结果:gfedcba;
方法二:
NSString * str = @"abcdefg";
NSMutableString * reverseString = [NSMutableString string];
for(int i = 0 ; i < str.length; i ++ ) {
char c = [str characterAtIndex:str.length- i -1];
[reverseString appendFormat:@"%c",c];
}
str = reverseString;
NSLog(@"%@",str);
结果:gfedcba;
两种方法比较而言,第一种方法比较优质,因为第一种方法str.length只调用了1次,而第二种方法调用了7次。效率提高了。
标签:
原文地址:http://www.cnblogs.com/wangliyuan/p/4744735.html