将Unichar Array转换为单个NSString

时间:2021-04-26 20:18:10

I created an unicode char array using 'characterAtIndex:' from an NSString object.

我使用NSString对象中的'characterAtIndex:'创建了一个unicode char数组。

I want to make the characters in the array again into a single complete string.

我想再次将数组中的字符组成一个完整的字符串。

Is there any method to convert this unicahr Array into NSStirng? Can anyone please help me???

有没有什么方法可以将这个unicahr数组转换成NSStirng?谁能帮帮我吗???

This s my code:

这是我的代码:

NSString *sentence = @"A long string";
NSString *new = [[NSString alloc]init];
unichar aBuffer[[sent length]];
int j=0;
//Tried to reverse the string:
for (int i=[sent length]-1; i>=0; i--,j++) {
    aBuffer[j]=[sent characterAtIndex:i];
    }

I found a better way for sting reversal than this, but let me know whether we have any method for this...

我找到了比这更好的反转方法,但让我知道我们是否有任何方法...

4 个解决方案

#1


2  

Is the array a standard c array? (looks something like this: array[50];) If it is, you can do this:

阵列是标准的c阵列吗? (看起来像这样:array [50];)如果是,你可以这样做:

[NSString stringWithCharacters:(const unichar*) length:(NSUInteger)];

#2


3  

You can convert to a unichar and back using getCharacters:range: and stringWithCharacters:.

您可以使用getCharacters转换为unichar并返回:range:和stringWithCharacters:。

NSString *sentence = @"A long unicode sentence";

NSUInteger length = [sentence length];
unichar aBuffer[length + 1];

[sentence getCharacters:aBuffer range:NSMakeRange(0, length)];
aBuffer[length] = 0x0;

NSString *newStr = [NSString stringWithCharacters:aBuffer length:length];

NSLog(@"%@", newStr);

#3


2  

Get a NSString from a bytes array with:

从字节数组中获取NSString:

- (id)initWithBytes:(const void *)bytes length:(NSUInteger)length encoding:(NSStringEncoding)encoding

(and you could use UTF8String instead of looping through the string with characterAtIndex:)

(你可以使用UTF8String而不是使用characterAtIndex循环遍历字符串:)

#4


0  

You can use Toll-Free Bridging to turn a char array into an objective-c class or reverse.

您可以使用Toll-Free Bridging将char数组转换为objective-c类或反向。

You can create a CFMutableStringRef variable with your array lets say the variable is myString. Than you can create a string or a mutable string with it.

您可以使用数组创建CFMutableStringRef变量,假设变量是myString。你可以用它创建一个字符串或一个可变的字符串。

NSMutableString* objcString = (NSMutableString*) myString;

#1


2  

Is the array a standard c array? (looks something like this: array[50];) If it is, you can do this:

阵列是标准的c阵列吗? (看起来像这样:array [50];)如果是,你可以这样做:

[NSString stringWithCharacters:(const unichar*) length:(NSUInteger)];

#2


3  

You can convert to a unichar and back using getCharacters:range: and stringWithCharacters:.

您可以使用getCharacters转换为unichar并返回:range:和stringWithCharacters:。

NSString *sentence = @"A long unicode sentence";

NSUInteger length = [sentence length];
unichar aBuffer[length + 1];

[sentence getCharacters:aBuffer range:NSMakeRange(0, length)];
aBuffer[length] = 0x0;

NSString *newStr = [NSString stringWithCharacters:aBuffer length:length];

NSLog(@"%@", newStr);

#3


2  

Get a NSString from a bytes array with:

从字节数组中获取NSString:

- (id)initWithBytes:(const void *)bytes length:(NSUInteger)length encoding:(NSStringEncoding)encoding

(and you could use UTF8String instead of looping through the string with characterAtIndex:)

(你可以使用UTF8String而不是使用characterAtIndex循环遍历字符串:)

#4


0  

You can use Toll-Free Bridging to turn a char array into an objective-c class or reverse.

您可以使用Toll-Free Bridging将char数组转换为objective-c类或反向。

You can create a CFMutableStringRef variable with your array lets say the variable is myString. Than you can create a string or a mutable string with it.

您可以使用数组创建CFMutableStringRef变量,假设变量是myString。你可以用它创建一个字符串或一个可变的字符串。

NSMutableString* objcString = (NSMutableString*) myString;