search

Home  >  Q&A  >  body text

objective-c - 关于NSString字符串反转的问题

今天看到这个“Objective-C分割NSString”,有人用正则表达式进行解答,感觉很特别。所以就把以前遇到的字符串反转的问你拿出来讨论讨论,可不可以用正则表达式来解决呢?

下面是个最简单的实现的如有什么效率问题,边界问题也希望多给意见。

/**** NSString+Reverse.h ****/
#import <Foundation/Foundation.h>
@interface NSString (Reverse)
- (NSString *)stringByReversed;
@end

/**** NSString+Reverse.m ****/
#import "NSString+Reverse.h"
@implementation NSString (Reverse)
- (NSString *)stringByReversed
{
  NSMutableString *s = [NSMutableString string];
  for (NSUInteger i=self.length; i>0; i--) {
    [s appendString:[self substringWithRange:NSMakeRange(i-1, 1)]];
  }
  return s;
}
@end
分割线

3月24日补充

根据@ParagonLight同学的回答我用如下代码做了个测试:

  NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
  [dateFormatter setDateFormat:@"ss.SSSS"];

  NSLog(@"S %@", [dateFormatter stringFromDate:[NSDate date]]);
  NSString *reversed = [string stringByReversed];
  NSLog(@"E %@", [dateFormatter stringFromDate:[NSDate date]]);

测试用的字符串长度为:8970

记录了10次结果,我制作了一个图表(算法1为我问题中的;算法2为@ParagonLight同学的)

只看代码的话算法2可以提高一半的效率,之所以花的时间比算法1高出许多,是因为每次循环都会生成一个新string对象,所耗费时间确实超乎我的预想了。

分割线

3月25日补充

由于算法1和算法3相差很小,为了让结果更明显,修改了测试用例,将测试字符串长度放大100倍,现在测试字符串长度为:897000

@ParagonLight同学的算法3的性能提升还是很明显的

巴扎黑巴扎黑2768 days ago665

reply all(1)I'll reply

  • PHP中文网

    PHP中文网2017-04-21 11:19:12

    I used the stringByReplacingCharactersInRange: method here. But this method actually creates a new string, so it doesn't feel like the most efficient approach. In fact, you can convert the string into a char array, then set two pointers to point to the head and tail of the array, and then exchange the pointed values ​​in sequence until i>j.
    I haven't been in contact with iOS for a long time, and I don't know much about its features. Please point out if there is anything wrong.

    - (NSString *)stringByReversed
    {
        NSUInteger i = 0;
        NSUInteger j = self.length - 1;
        NSString *temp;
        NSString *newString = self;
        NSLog(@"%@", self);
        while (i < j) {
            temp = [newString substringWithRange:NSMakeRange(i, 1)];
            newString = [newString stringByReplacingCharactersInRange:NSMakeRange(i, 1) withString:[self substringWithRange:NSMakeRange(j, 1)]];
            newString = [newString stringByReplacingCharactersInRange:NSMakeRange(j, 1) withString:temp];
            NSLog(@"%@",newString);
            i ++;
            j --;
        }
        NSLog(@"%@", newString);
        return newString;
    }
    Separating line

    Try this one

    -(NSString *)stringByReversed{
        NSUInteger i = 0;
        NSUInteger j = self.length - 1;
        unichar characters[self.length];
        while (i < j) {
            characters[j] = [self characterAtIndex:i];
            characters[i] = [self characterAtIndex:j];
            i ++;
            j --;
        }
        if(i == j)
            characters[i] = [self characterAtIndex:i];
        return [NSString stringWithCharacters:characters length:self.length];
    }
    Update: Algorithm 4

    Visual inspection indicates that the unichar array is out of bounds. Just malloc directly. . . But I have to say, this is no longer OC. . .

    - (NSString *)stringByReversed{
      uint64_t  i = 0;
      uint64_t j = self.length - 1;
    //  unichar characters[self.length];
      unichar *characters = malloc(sizeof([self characterAtIndex:0]) * self.length);
      while (i < j) {
        characters[j] = [self characterAtIndex:i];
        characters[i] = [self characterAtIndex:j];
        i ++;
        j --;
      }
      if(i == j)
        characters[i] = [self characterAtIndex:i];
      return [NSString stringWithCharacters:characters length:self.length];
    }

    reply
    0
  • Cancelreply