反转NSString文本

时间:2011-07-16 20:56:06

标签: objective-c cocoa nsstring

我一直在谷歌搜索如何做到这一点,但我将如何扭转NSString?例如:你好了:ih

我正在寻找最简单的方法。

谢谢!

@Vince我做了这个方法:

- (IBAction)doneKeyboard {

// first retrieve the text of textField1
NSString *myString = field1.text;
NSMutableString *reversedString = [NSMutableString string];
NSUInteger charIndex = 0;
while(myString && charIndex < [myString length]) {
    NSRange subStrRange = NSMakeRange(charIndex, 1);
    [reversedString appendString:[myString substringWithRange:subStrRange]];
    charIndex++;
}
// reversedString is reversed, or empty if myString was nil
field2.text = reversedString;
}

我把这个方法连接到textfield1的didendonexit。当我单击完成按钮时,它不会反转文本,UILabel只显示我输入的UITextField文本。有什么问题?

19 个答案:

答案 0 :(得分:103)

阻止版本。

NSString *myString = @"abcdefghijklmnopqrstuvwxyz";
NSMutableString *reversedString = [NSMutableString stringWithCapacity:[myString length]];

[myString enumerateSubstringsInRange:NSMakeRange(0,[myString length]) 
                             options:(NSStringEnumerationReverse | NSStringEnumerationByComposedCharacterSequences)
                          usingBlock:^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop) {
                            [reversedString appendString:substring];
                        }];

// reversedString is now zyxwvutsrqponmlkjihgfedcba

答案 1 :(得分:51)

写一个简单的循环来做到这一点:

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

在您的情况下:

// first retrieve the text of textField1
NSString *myString = textField1.text;
NSMutableString *reversedString = [NSMutableString string];
NSInteger charIndex = [myString length];
while (myString && charIndex > 0) {
    charIndex--;
    NSRange subStrRange = NSMakeRange(charIndex, 1);
    [reversedString appendString:[myString substringWithRange:subStrRange]];
}
// reversedString is reversed, or empty if myString was nil
textField2.text = reversedString;

答案 2 :(得分:10)

jano的回答是正确的。不幸的是,它会产生许多不必要的临时对象。这是一个更快(更复杂)的实现,基本上做同样的事情,但使用memcpy和unichar缓冲区将内存分配保持在最低限度。

- (NSString *)reversedString
{
    NSUInteger length = [self length];
    if (length < 2) {
        return self;
    }

    unichar *characters = calloc(length, sizeof(unichar));
    unichar *reversedCharacters = calloc(length, sizeof(unichar));
    if (!characters || !reversedCharacters) {
        free(characters);
        free(reversedCharacters);
        return nil;
    }

    [self getCharacters:characters range:NSMakeRange(0, length)];

    NSUInteger i = length - 1;
    NSUInteger copiedCharacterCount = 0;

    // Starting from the end of self, copy each composed character sequence into reversedCharacters
    while (copiedCharacterCount < length) {
        NSRange characterRange = [self rangeOfComposedCharacterSequenceAtIndex:i];
        memcpy(reversedCharacters + copiedCharacterCount, characters + characterRange.location, characterRange.length * sizeof(unichar));
        i = characterRange.location - 1;
        copiedCharacterCount += characterRange.length;
    }

    free(characters);

    NSString *reversedString = [[NSString alloc] initWithCharactersNoCopy:reversedCharacters length:length freeWhenDone:YES];
    if (!reversedString) {
        free(reversedCharacters);
    }

    return reversedString;
}

我在100,000个随机多字节Unicode字符串上测试了这个,长度在1到128之间。这个版本比jano快了4-5倍。

Enumerate substrings: 2.890528
MemCopy: 0.671090
Enumerate substrings: 2.840411
MemCopy: 0.662882

测试代码位于https://gist.github.com/prachigauriar/9739805

更新:我只需转换为UTF-32缓冲区并将其反转即可再次尝试此操作。

- (NSString *)qlc_reversedStringWithUTF32Buffer
{
    NSUInteger length = [self length];
    if (length < 2) {
        return self;
    }

    NSStringEncoding encoding = NSHostByteOrder() == NS_BigEndian ? NSUTF32BigEndianStringEncoding : NSUTF32LittleEndianStringEncoding;
    NSUInteger utf32ByteCount = [self lengthOfBytesUsingEncoding:encoding];
    uint32_t *characters = malloc(utf32ByteCount);
    if (!characters) {
        return nil;
    }

    [self getBytes:characters maxLength:utf32ByteCount usedLength:NULL encoding:encoding options:0 range:NSMakeRange(0, length) remainingRange:NULL];

    NSUInteger utf32Length = utf32ByteCount / sizeof(uint32_t);
    NSUInteger halfwayPoint = utf32Length / 2;
    for (NSUInteger i = 0; i < halfwayPoint; ++i) {
        uint32_t character = characters[utf32Length - i - 1];
        characters[utf32Length - i - 1] = characters[i];
        characters[i] = character;
    }

    return [[NSString alloc] initWithBytesNoCopy:characters length:utf32ByteCount encoding:encoding freeWhenDone:YES];
}

这比memcpy版本快3-4倍。上述要点已使用最新版本的代码进行了更新。

Enumerate substrings: 2.168705
MemCopy: 0.488320
UTF-32: 0.150822
Enumerate substrings: 2.169655
MemCopy: 0.481786
UTF-32: 0.147534
Enumerate substrings: 2.248812
MemCopy: 0.505995
UTF-32: 0.154531

答案 3 :(得分:8)

我想我会抛出另一个版本以防万一有兴趣..个人而言,我喜欢使用NSMutableString的更干净的方法,但如果性能是最高优先级,那么这个更快:

- (NSString *)reverseString:(NSString *)input {
    NSUInteger len = [input length];
    unichar *buffer = malloc(len * sizeof(unichar));
    if (buffer == nil) return nil; // error!
    [input getCharacters:buffer];

    // reverse string; only need to loop through first half
    for (NSUInteger stPos=0, endPos=len-1; stPos < len/2; stPos++, endPos--) {
        unichar temp = buffer[stPos];
        buffer[stPos] = buffer[endPos];
        buffer[endPos] = temp;
    }

    return [[NSString alloc] initWithCharactersNoCopy:buffer length:len freeWhenDone:YES];
}

我还写了一个快速测试,将它与更传统的NSMutableString方法(我在下面也包括在内)进行比较:

// test reversing a really large string
NSMutableString *string = [NSMutableString new];
for (int i = 0; i < 10000000; i++) {
    int digit = i % 10;
    [string appendFormat:@"%d", digit];
}
NSTimeInterval startTime = [[NSDate date] timeIntervalSince1970];
NSString *reverse = [self reverseString:string];
NSTimeInterval elapsedTime = [[NSDate date] timeIntervalSince1970] - startTime;
NSLog(@"reversed in %f secs", elapsedTime);

结果是:

  • 使用NSMutableString方法(如下) - &#34;在3.720631秒内反转&#34;

  • 使用unichar *缓冲方法(上图) - &#34;在0.032604秒内反转&#34;

仅供参考,这里是用于此比较的NSMutableString方法:

- (NSString *)reverseString:(NSString *)input {
    NSUInteger len = [input length];
    NSMutableString *result = [[NSMutableString alloc] initWithCapacity:len];
    for (int i = len - 1; i >= 0; i--) {
        [result appendFormat:@"%c", [input characterAtIndex:i]];
    }
    return result;
}

答案 4 :(得分:3)

对任何对象使用方法:NSString,NSNumber等。:

NSLog(@"%@",[self reverseObject:@12345]);
NSLog(@"%@",[self reverseObject:@"Hello World"]);

方法:

-(NSString*)reverseObject:(id)string{

string = [NSString stringWithFormat:@"%@",string];
NSMutableString *endString = [NSMutableString new];

while ([string length]!=[endString length]) {
    NSRange range = NSMakeRange([string length]-[endString length]-1, 1);
    [endString appendString: [string substringWithRange:range]];
}
return endString;}

日志:

2014-04-16 11:20:25.312 TEST[23733:60b] 54321
2014-04-16 11:20:25.313 TEST[23733:60b] dlroW olleH

答案 5 :(得分:2)

如果你只迭代超过一半的字符串交换两端的字符会更快吗?因此,对于5个字符的字符串,您可以交换字符1 + 5,然后2 + 4和3不需要与任何东西交换。

NSMutableString *reversed = [original mutableCopyWithZone:NULL];
NSUInteger i, length;

length = [reversed length];

for (i = 0; i < length / 2; i++) {
    // Store the first character as we're going to replace with the character at the end
    // in the example, it would store 'h' 
    unichar startChar = [reversed characterAtIndex:i];

    // Only make the end range once
    NSRange endRange = NSMakeRange(length - i, 1);

    // Replace the first character ('h') with the last character ('i')
    // so reversed now contains "ii"
    [reversed replaceCharactersInRange:NSMakeRange(i, 1) 
                            withString:[reversed subStringWithRange:endRange];

    // Replace the last character ('i') with the stored first character ('h)
    // so reversed now contains "ih"
    [reversed replaceCharactersInRange:endRange
                            withString:[NSString stringWithFormat:@"%c", startChar]];
}

编辑----

完成了一些测试后,答案是“否”,它比循环所有内容的版本慢约6倍。减慢我们速度的事情是为replaceCharactersInRange:withString方法创建临时NSStrings。这是一种通过直接操作字符数据只创建一个NSString的方法,在简单测试中似乎要快得多。

NSUInteger length = [string length];
unichar *data = malloc(sizeof (unichar) * length);
int i;

for (i = 0; i < length / 2; i++) {
    unichar startChar = [string characterAtIndex:i];
    unichar endChar = [string  characterAtIndex:(length - 1) - i];

    data[i] = endChar;
    data[(length - 1) - i] = startChar;
}

NSString *reversed = [NSString stringWithCharacters:data length:length];
free(data);

答案 6 :(得分:2)

Swift 2.0:

1)让str =&#34;你好,世界!&#34;         let reversed = String(str.characters.reverse())         打印(反转)

简而言之:

String("This is a test string.".characters.reverse())

2)

let string = "This is a test string."
let characters = string.characters
let reversedCharacters = characters.reverse()
let reversedString = String(reversedCharacters)

简短的方法:

String("This is a test string.".characters.reverse())

OR

let string = "This is a test string."
let array = Array(string)
let reversedArray = array.reverse()
let reversedString = String(reversedArray)
The short way :

String(Array("This is a test string.").reverse())

在Play Ground上测试:

import Cocoa

//Assigning a value to a String variable
var str = "Hello, playground"

//Create empty character Array.
var strArray:Character[] = Character[]()

//Loop through each character in the String
for character in str {
//Insert the character in the Array variable.
strArray.append(character)
}

//Create a empty string
var reversedStr:String = ""

//Read the array from backwards to get the characters
for var index = strArray.count - 1; index >= 0;--index {
//Concatenate character to String.
reversedStr += strArray[index]
}

较短的版本:

var str = “Hello, playground”
var reverseStr = “”
for character in str {
reverseStr = character + reverseStr
}

答案 7 :(得分:1)

使用递归反转字符串:

@implementation NSString (Reversed)

+ (NSString *)reversedStringFromString:(NSString *)string
{
    NSUInteger count = [string length];

    if (count <= 1) { // Base Case
        return string;
    } else {
        NSString *lastLetter = [string substringWithRange:NSMakeRange(count - 1, 1)];
        NSString *butLastLetter = [string substringToIndex:count - 1];
        return [lastLetter stringByAppendingString:[self reversedStringFromString:butLastLetter]];
    }
}

@end

答案 8 :(得分:1)

这些答案似乎都没有考虑多字节字符,所以这里是我的示例代码。它假设你只传入一个长于一个字符的字符串。

- (void)testReverseString:(NSString *)string
{
    NSMutableString *rString = [NSMutableString new];
    NSInteger extractChar = [string length] - 1;
    while (extractChar >= 0)
    {
        NSRange oneCharPos = [string rangeOfComposedCharacterSequenceAtIndex:extractChar];
        for (NSUInteger add = 0; add < oneCharPos.length; ++ add)
        {
            unichar oneChar = [string characterAtIndex:oneCharPos.location + add];
            [rString appendFormat:@"%C", oneChar];
        }
        extractChar -= oneCharPos.length;
    }

    NSLog(@"%@ becomes %@", string, encryptedString );
}

答案 9 :(得分:1)

  • NSString into char utf32(总是32位(unsigned int))
  • 反向
  • char utf32 into NSString

+ (NSString *)reverseString3:(NSString *)str {
    unsigned int *cstr, buf, len = [str length], i;  
    cstr  = (unsigned int *)[str cStringUsingEncoding:NSUTF32LittleEndianStringEncoding];
    for (i=0;i < len/2;i++) buf = cstr[i], cstr[i] = cstr[len -i-1], cstr[len-i-1] = buf;
    return [[NSString alloc] initWithBytesNoCopy:cstr length:len*4 encoding:NSUTF32LittleEndianStringEncoding freeWhenDone:NO];
}

示例:Apple_is---&gt; si_elppA

答案 10 :(得分:1)

NSMutableString *result = [NSMutableString stringWithString:@""];
    for (long i = self.length - 1; i >= 0; i--) {
        [result appendFormat:@"%c", [self characterAtIndex:i]];
    }
    return (NSString *)result;

答案 11 :(得分:0)

我们也可以按如下方式实现反向字符串。

NSString *originalString = @"Hello";
NSString *reverseString;
for (NSUInteger index = originalString.length; index > 0; index--) {
    char character = [originalString characterAtIndex:index];
    reverseString = [reverseString stringByAppendingString:[NSString stringWithFormat:@"%c", character]];
}

NSString *originalString = @"Hello";
NSString *reverseString;
for (NSUInteger index = originalString.length; index > 0; index--) {
   char *character = [originalString characterAtIndex:index];
   reverseString = [reverseString stringByAppendingString:[NSString stringWithFormat:@"%s", character]];
}

答案 12 :(得分:0)

Google is your friend

-(NSString *) reverseString
{
  NSMutableString *reversedStr;
  int len = [self length];

  // Auto released string
  reversedStr = [NSMutableString stringWithCapacity:len];     

  // Probably woefully inefficient...
  while (len > 0)
    [reversedStr appendString:
         [NSString stringWithFormat:@"%C", [self characterAtIndex:--len]]];   

  return reversedStr;
}

答案 13 :(得分:0)

夫特:

let string = "reverse"
let reversedStringCollection = string.characters.reversed()

for character in reversedStringCollection {
    reversedString.append(character)
    print(reversedString)
}

答案 14 :(得分:0)

以下是Objective-C中的类别集合,它们将反转NSStrings和NSAttributedStrings(同时保留字符属性):TextFlipKit

例如:

NSString *example = @"Example Text";

NSString *reversed = example.tfk_reversed;

NSLog(@"Reversed: %@", reversed);

//prints 'Reversed: txeT elpmaxE'

答案 15 :(得分:-1)

str=@"india is my countery";
array1=[[NSMutableArray alloc] init];
for(int i =0 ;i<[str length]; i++) {
    NSString *singleCharacter  = [NSString stringWithFormat:@"%c", [str characterAtIndex:i]];
    [array1 addObject:singleCharacter];
}


NSMutableString* theString = [NSMutableString string];

for (int i=[array1 count]-1; i>=0;i--){
    [theString appendFormat:@"%@",[array1 objectAtIndex:i]];
}

答案 16 :(得分:-1)

为NSString添加一个类别,以便将来可以在任何NSString上调用reverse,如下所示:

#import "NSString+Reverse.h"

@implementation NSString (Reverse)
-(NSString*)reverse {
  char* cstring = (char*)[self UTF8String];
  int length = [self length]-1;

  int i=0;

  while (i<=length) {
    unichar tmp = cstring[i];
    cstring[i] = cstring[length];
    cstring[length] = tmp;
    i++;
    length--;
  }

  return [NSString stringWithCString:cstring encoding:NSUTF8StringEncoding];

}
@end

答案 17 :(得分:-2)

为此目的,我有两个简单的解决方案:

+(NSString*)reverseString:(NSString *)str
{
    NSMutableString* reversed = [NSMutableString stringWithCapacity:str.length];
    for (int i = (int)str.length-1; i >= 0; i--){
         [reversed appendFormat:@"%c", [str characterAtIndex:i]];
    }
    return reversed;
}

+(NSString*)reverseString2:(NSString *)str
{
    char* cstr = (char*)[str UTF8String];
    int len = (int)str.length;
    for (int i =  0; i < len/2; i++) {
        char buf = cstr[i];
        cstr[i] = cstr[len-i-1];
        cstr[len-i-1] = buf;
    }
    return [[NSString alloc] initWithBytes:cstr length:len encoding:NSUTF8StringEncoding];
}

现在,让我们测试吧!

NSString* str = @"Objective-C is a general-purpose, object-oriented programming language that adds Smalltalk-style messaging to the C programming language";
NSLog(@"REV 1: %@", [Util reverseString:str]);
start = [NSDate date];
for (int i = 0 ; i < 1000; ++i)
    [Util reverseString:str];
end = [NSDate date];
NSLog(@"Time per 1000 repeats: %f", [end timeIntervalSinceDate:start]);

NSLog(@"REV 2: %@", [Util reverseString2:str]);
start = [NSDate date];
for (int i = 0 ; i < 1000; ++i)
    [Util reverseString2:str];
end = [NSDate date];
NSLog(@"Time per 1000 repeats: %f", [end timeIntervalSinceDate:start]);

结果:

ConsoleTestProject[68292:303] REV 1: egaugnal gnimmargorp C eht ot gnigassem elyts-klatllamS sdda taht egaugnal gnimmargorp detneiro-tcejbo ,esoprup-lareneg a si C-evitcejbO
ConsoleTestProject[68292:303] Time per 1000 repeats: 0.063880
ConsoleTestProject[68292:303] REV 2: egaugnal gnimmargorp C eht ot gnigassem elyts-klatllamS sdda taht egaugnal gnimmargorp detneiro-tcejbo ,esoprup-lareneg a si C-evitcejbO
ConsoleTestProject[68292:303] Time per 1000 repeats: 0.002038

更多的结果是:

ConsoleTestProject[68322:303] chars: 1982
ConsoleTestProject[68322:303] Time 1 per 1000 repeats: 1.014893
ConsoleTestProject[68322:303] Time 2 per 1000 repeats: 0.024928

与上述功能相同的文字:

ConsoleTestProject[68366:303] Time 1 per 1000 repeats: 0.873574
ConsoleTestProject[68366:303] Time 2 per 1000 repeats: 0.019300
ConsoleTestProject[68366:303] Time 3 per 1000 repeats: 0.342735 <-Vladimir Gritsenko
ConsoleTestProject[68366:303] Time 4 per 1000 repeats: 0.584012 <- Jano

所以,选择表现!

答案 18 :(得分:-2)

我写了一个类别:D

//的NSString + Reversed.h     #import

//
//  NSString+Reversed.h
//  HTMLPageFormatter
//  Created by beit46 on 21.06.13.
//  

@interface NSString (Reversed)
- (NSString *)reversedString;
@end

//的NSString + Reversed.m

//
//  NSString+Reversed.m
//  HTMLPageFormatter
//  Created by beit46 on 21.06.13.
#import "NSString+Reversed.h"

@implementation NSString (Reversed)
- (NSString *)reversedString {
    NSMutableString *reversedString = [NSMutableString stringWithCapacity:[self length]];

    [self enumerateSubstringsInRange:NSMakeRange(0,[self length])
                                 options:(NSStringEnumerationReverse | NSStringEnumerationByComposedCharacterSequences)
                              usingBlock:^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop) {
                                  [reversedString appendString:substring];
                              }];
    return [reversedString copy];
}
@end