如何重新排列NSString字符?

时间:2013-11-18 07:55:20

标签: ios nsstring

我有一个NSString,

11/17/2013 12:00:00 AM

我想让它像

17/11/2013 12:00:00 AM

我想用Day刷日,

我试图通过subString来做,但无法得到确切的想法。

有一种方法可以转换为NSData并更改格式,但我更喜欢通过NSString。

由于

3 个答案:

答案 0 :(得分:3)

试一试:

NSMutableArray *dateArray = [[@"11/17/2013 12:00:00 AM" componentsSeparatedByString:@"/"] mutableCopy];
[dateArray exchangeObjectAtIndex:1 withObjectAtIndex:0];
NSString *dateString = [dateArray componentsJoinedByString:@"/"];
NSLog(@"%@", dateString);

答案 1 :(得分:1)

这样的事情:

NSString *date = @"11/17/2013 12:00:00 AM";
NSString *m = [date substringWithRange:NSMakeRange(0, 2)];
NSString *d = [date substringWithRange:NSMakeRange(3, 2)];
NSString *result = [NSString stringWithFormat:@"%@/%@/2013 12:00:00 AM",d,m];

我还没有测试过它

答案 2 :(得分:-1)

你可以这样做:

NSMutableArray *array = [[yourString componentsSeparatedByString:@"/"] mutableCopy];
id temp = [array objectAtIndex:1];
[array replaceObjectAtIndex:1 withObject:[array objectAtIndex:0]];
[array replaceObjectAtIndex:0 withObject:temp];

NSString * finalString = [array componentsJoinedByString:@"/"];