1

我想创建一个提供字符串反转的方法。假设我在方法中传递了一个 NSString“Welcome to Objective C”,并且该方法返回一个字符串的反转,如“C Objective to Welcome”而不是“C evitcejbO ot emocleW”而不使用componentsSeparatedByString 方法。是否有可能与目标 c ..?请帮忙。

4

6 回答 6

6

您可以逐字枚举字符串。

NSString *string = @"Welcome to Objective-C!";

NSMutableArray *words = [NSMutableArray array];

[string enumerateLinguisticTagsInRange:NSMakeRange(0, [string length])
                                scheme:NSLinguisticTagSchemeTokenType
                               options:0
                           orthography:nil
                            usingBlock:^(NSString *tag, NSRange tokenRange, NSRange sentenceRange, BOOL *stop) {
                                [array addObject:[string substringWithRange:tokenRange]];
                            }];

NSMutableString *reverseString = [[NSMutableString alloc] init];

for (NSString *word in [words reverseObjectEnumerator]){
    [reverse appendString:word];
}

NSLog(@"%@", reverseString);

这将打印...

"!C-Objective to Welcome"

您可以更改选项以省略空格和其他内容...

于 2013-05-09T10:36:34.037 回答
2

我使用以下方法在 iOS 中反转字符串

- (NSString *)reverseString:(NSString *)stringToReverse
{
    NSMutableString *reversedString = [NSMutableString stringWithCapacity:[stringToReverse length]];
    [stringToReverse enumerateSubstringsInRange:NSMakeRange(0, [stringToReverse length])
                                        options:(NSStringEnumerationReverse | NSStringEnumerationByComposedCharacterSequences)
                                     usingBlock:^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop) {
            [reversedString appendString:substring];
    }];
    return reversedString;
}
于 2015-03-17T10:48:55.043 回答
1

对不起,我之前误读了你的问题。我使用一系列循环来做到这一点,我的答案比 Fogmeister 更混乱,但我想试一试,看看我是否能做到。

NSString *str = @"This is a test";


    NSMutableArray *array = [[NSMutableArray alloc] init];

    for(int i = 0; i < [str length]; i++)
    {
        char sTest = [str characterAtIndex:i];
        if(sTest == ' ')
        {
            [array addObject:[NSNumber numberWithInt:i]];
        }
    }

    NSInteger iNext = [[array objectAtIndex:[array count]-1] integerValue];
    iNext+=1;

    if(iNext < [str length])
    {
       [array addObject:[NSNumber numberWithInt:iNext]]; 
    }

    NSMutableArray *wordArray = [[NSMutableArray alloc] init];

    for(int i = 0; i < [array count]; i++)
    {
        if (i == 0)
        {
            int num = [[array objectAtIndex:i] integerValue];
            NSString *s = [[str substringFromIndex:0] substringToIndex:num];
            [wordArray addObject:s];

        }
        else if(i == [array count]-1)
        {
            int prev = [[array objectAtIndex:i-1] integerValue]+1;
            int num =  [str length];
            NSString *s = [[str substringToIndex:num] substringFromIndex:prev];
            [wordArray addObject:s];
        }
        else
        {
            int prev = [[array objectAtIndex:i-1] integerValue]+1;
            int num = [[array objectAtIndex:i] integerValue];

            NSString *s = [[str substringToIndex:num] substringFromIndex:prev];
            [wordArray addObject:s];
        }
    }

    NSMutableArray *reverseArray = [[NSMutableArray alloc]init];
    for(int i = [wordArray count]-1; i >= 0; i--)
    {

        [reverseArray addObject:[wordArray objectAtIndex:i]];
    }
    NSLog(@"%@", reverseArray);
于 2013-05-09T11:07:22.470 回答
1

试试这个,它按照您的期望完美运行,

调用函数:-

 [self reversedString:@"iOS"];

反转字符串函数:-

 -(void)reversedString :(NSString *)reversStr

 {    // reversStr is "iOS"

     NSMutableString *reversedString = [NSMutableString string];
     NSInteger charIndex = [reversStr length];
     while (charIndex > 0) {
     charIndex--;
     NSRange subStrRange = NSMakeRange(charIndex, 1);
    [reversedString appendString:[reversStr substringWithRange:subStrRange]];
  }
   NSLog(@"%@", reversedString); // outputs "SOi"
 }

希望所以这对某些人有帮助。

于 2017-09-21T10:33:53.357 回答
1

在这里,我用最少的循环次数替换了字符。日志(n/2)。

NSString  *string=@"Happy World";
NSInteger lenth=[string length];
NSInteger halfLength=[string length]/2;

for(int i=0;i<halfLength;i++)
{
   NSString *leftString=[NSString stringWithFormat:@"%c",[string characterAtIndex:i]];
   NSString *rightString=[NSString stringWithFormat:@"%c",[string characterAtIndex:(lenth-i-1)]];

   string= [string stringByReplacingCharactersInRange:NSMakeRange(i, 1) withString:rightString];
   string=[string stringByReplacingCharactersInRange:NSMakeRange((lenth-i-1), 1) withString:leftString];

}

NSLog(@"%@",string);
于 2017-01-07T11:17:11.283 回答
0

There is no API to do that, if that's what you are asking.

You can always iterate through the string looking for white spaces (or punctuation, it depends on your needs), identify the words and recompose your "reversed" message manually.

于 2013-05-09T10:28:23.550 回答