可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
Does anyone know of an existing way to change the order of an existing NSString or NSMutableString's characters? I have a workaround in mind anyway but it would be great if there was an existing method for it.
For example, given the string @"HORSE", a method which would return @"ORSEH", @"SORHE", @"ROHES", etc?
回答1:
Consider this code:
.h File:
@interface NSString (Scrambling) + (NSString *)scrambleString:(NSString *)toScramble; @end
.m File:
@implementation NSString (Scrambling) + (NSString *)scrambleString:(NSString *)toScramble { for (int i = 0; i < [toScramble length] * 15; i ++) { int pos = arc4random() % [toScramble length]; int pos2 = arc4random() % ([toScramble length] - 1); char ch = [toScramble characterAtIndex:pos]; NSString *before = [toScramble substringToIndex:pos]; NSString *after = [toScramble substringFromIndex:pos + 1]; NSString *temp = [before stringByAppendingString:after]; before = [temp substringToIndex:pos2]; after = [temp substringFromIndex:pos2]; toScramble = [before stringByAppendingFormat:@"%c%@", ch, after]; } return toScramble; } @end
Not the most beautiful code or execution, but gets the job done. There's probably a (const char *) way to do this, but this works fine for me. A quick test shows a 0.001021 second length for execution on my Mac.
Usage:
NSString *scrambled = [NSString scrambleString:otherString];
Code adapted from another language / pseudocode
回答2:
You can use Durstenfeld's variation of the Fisher-Yates Shuffle.
For a very long string, you could save a lot of CPU time and allocations by copying the unichars to a unichar buffer, then performing the transform using a c or c++ approach to swap characters. Note that the UTF8String is not the buffer you want to take, nor should you mutate it. Then create (or set) a new NSString from the shuffled buffer.
More info on the Fisher Yates algo and C and C++ implementations can be found here.