// // main.m // 字符反转 #import <Foundation/Foundation.h> #import "NSString+fanZhuan.h" int main(int argc, const char * argv[]) { @autoreleasepool {
NSMutableString *str1=[NSMutableString stringWithString:@"我实数1234567语言环境agb"]; [str1 fanZhuan]; NSLog(@"%@",[str1 fanZhuan]); } return 0; }
// NSString+fanZhuan.h
#import <Foundation/Foundation.h> @interface NSString (fanZhuan) -(instancetype)fanZhuan; @end
// NSString+fanZhuan.m #import "NSString+fanZhuan.h" @implementation NSString (fanZhuan) -(instancetype)fanZhuan{ NSMutableString *str = [[NSMutableString alloc] init]; //将字符串倒序追加到另一个可变字符串中,返回str即可 for (int i=(int)self.length-1; i>=0; i--) { NSString* s=[self substringWithRange:NSMakeRange(i, 1)];//截取指定范围的字符串 [str appendString:[NSString stringWithString:s]];//追加字符串 } return str; }
@end
|