反转数组目标c中的字符串

时间:2013-01-21 14:23:16

标签: iphone objective-c

  

可能重复:
  Reverse NSString text

我是objectovec的新手。我有一个字符串数组。如何反转每个字符串?

NSArrayNSstring的哪种方法会帮助我?

我想在数组中反转字符串。

感谢

5 个答案:

答案 0 :(得分:3)

创建一个返回反向字符串的方法。

-(NSString *)reverseString:(NSString *)string{
    NSString *reverseString=[NSString new];
    for (NSInteger i=string.length-1; i>-1; i--) {
        reverseString=[reverseString stringByAppendingFormat:@"%c",[string characterAtIndex:i]];
    }
    return reverseString;
}

在你的任何方法中:

NSMutableArray *names=[NSMutableArray arrayWithObjects:@"anoop",@"johnson",@"wasim",nil];
for (NSInteger i=0; i<names.count; i++) {
    names[i]=[self reverseString:names[i]];
}
NSLog(@"%@",names);

答案 1 :(得分:1)

NSString *str=textEntered.text;//
    NSMutableArray *temp=[[NSMutableArray alloc] init];
    for(int i=0;i<[str length];i++)
    {
        [temp addObject:[NSString stringWithFormat:@"%c",[str characterAtIndex:i]]];
    }
    temp = [NSMutableArray arrayWithArray:[[temp reverseObjectEnumerator] allObjects]];
    NSString *reverseString=@"";
    for(int i=0;i<[temp count];i++)
    {
        reverseString=[NSString stringWithFormat:@"%@%@",reverseString,[temp objectAtIndex:i]];
    }
    NSLog(@"%@",reverseString);

反向字符串的其他方式 NSStringEnumerationOptions

- (NSString *)reverseString:(NSString *)string {
    NSMutableString *reversedString = [[NSMutableString alloc] init];
    NSRange fullRange = [string rangeOfString:string];
    NSStringEnumerationOptions enumerationOptions = (NSStringEnumerationReverse | NSStringEnumerationByComposedCharacterSequences);
    [string enumerateSubstringsInRange:fullRange options:enumerationOptions usingBlock:^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop) {
        [reversedString appendString:substring];
    }];
    return reversedString;
}

答案 2 :(得分:1)

希望这个帮助

NSString *str=textEntered.text;//
    NSMutableArray *temp=[[NSMutableArray alloc] init];
    for(int i=0;i<[str length];i++)
    {
        [temp addObject:[NSString stringWithFormat:@"%c",[str characterAtIndex:i]]];
    }
    temp = [NSMutableArray arrayWithArray:[[temp reverseObjectEnumerator] allObjects]];
    NSString *reverseString=@"";
    for(int i=0;i<[temp count];i++)
    {
        reverseString=[NSString stringWithFormat:@"%@%@",reverseString,[temp objectAtIndex:i]];
    }
    NSLog(@"%@",reverseString);

答案 3 :(得分:0)

使用

unichar c[yourstring.Length];
    NSRange raneg={0,yourstring.Length};
    [yourstring getCharacters:c range:raneg];
//c will be an array of your string do what ever you wish

答案 4 :(得分:0)

使用此字符串反转字符串,然后以相同的索引存储回数组。

-(NSString*)reverseString:(NSString*)string {
    NSMutableString *reversedString;
    int length = [string length];
    reversedString = [NSMutableString stringWithCapacity:length];
    while (length--) {
            [reversedString appendFormat:@"%c", [string characterAtIndex:length]];
    }
    return reversedString;
}

但是如果您有可变字符串,那么您可以创建一个类别

@interface NSMutableString (Reverse)
- (void)reverseString;
@end

@implementation NSMutableString (Reverse)

- (void)reverseString {
    for (int i = 0; i < self.length/2; i++) {
        int l = self.length - 1 - i;

        NSRange iRange = NSMakeRange(i, 1);
        NSRange lRange = NSMakeRange(l, 1);

        NSString *iStr = [self substringWithRange:iRange];
        NSString *lStr = [self substringWithRange:lRange];

        [self replaceCharactersInRange:iRange withString:lStr];
        [self replaceCharactersInRange:lRange withString:iStr];
    }
}

@end

然后您可以像这样使用此类别方法

NSArray *arr = [NSArray arrayWithObjects:[@"hello" mutableCopy], [@"Do it now" mutableCopy], [@"Test string, 123 123" mutableCopy], nil];
NSLog(@"%@",arr);
[arr makeObjectsPerformSelector:@selector(reverseString)];
NSLog(@"%@",arr);