第一种方法:(注意中文字符串的处理)
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[])
{
NSString *[email protected]"我是一个字符串";
NSMutableString *str1=[NSMutableString string];
NSMutableString *str2=[NSMutableString stringWithUTF8String:[str UTF8String]];
int i=0;
while (i!=[str2 length])
{
NSString *str3=[str2 substringWithRange:NSMakeRange([str2 length]-i-1, 1)];
[str1 appendFormat:@"%@",str3];
i++;
}
NSLog(@"str1:%@",str1);
return 0;
}
第二种方法:(注意中文字符串的处理)
int main(int argc, const char * argv[])
{
NSString *str = @"我是一个字符串";
NSMutableString *str1=[NSMutableString string];
int i=0;
NSMutableString *str2=[NSMutableString stringWithUTF8String:[str UTF8String]];
NSLog(@"str2:%@",str2);
while (i!=[str2 length])
{
unichar ch=[str2 characterAtIndex:[str2 length]-i-1];
NSString *str4=[NSString stringWithCharacters:&ch length:1];
[str1 appendString:str4];
i++;
}
NSLog(@"str1:%@",str1);
return 0;
}