练习题1
// // 1.整数123456789,如何将这个整数的每一位数,从末位开始依次放入一个数组中,并将他们遍历出来 // 2.如何将字符串@“abc123.xyz789”倒置 // 3.将2013年05月05日转换成2013-05-05 #import <Foundation/Foundation.h> int main(int argc, const char * argv[]) { @autoreleasepool { NSLog(@"第一题"); NSInteger num=123456789; //NSNumber *newnum=@(num); NSString *strnum=[[NSString alloc]initWithFormat:@"%ld",num]; NSMutableArray *arr=[NSMutableArray array]; for (int i=8; i>=0; i--) { NSRange range; range.length=1; range.location=i; NSString *str=[NSString alloc]; str=[strnum substringWithRange:range]; [arr addObject:str]; } for (id str1 in arr) { NSLog(@"%@",str1); } NSLog(@"第二题"); NSString *string=@"abc123.xyz789"; NSMutableString *newString=[[NSMutableString alloc]initWithCapacity:string.length]; for (long i=string.length-1; i>=0; i--) { [newString appendFormat:@"%c",[string characterAtIndex:i]]; } NSLog(@"%@",newString); NSLog(@"第三题"); NSString *date=@"2013年05月05日"; NSMutableString *Date=[NSMutableString stringWithString:date]; NSRange rangeDate; int i=4; while (i<9) { rangeDate.length=1; rangeDate.location=i; [Date replaceCharactersInRange:rangeDate withString:@"-"]; i+=3; } NSString *newdate=[Date substringToIndex:i]; NSLog(@"%@",newdate); } return 0; }