简体   繁体   English

将Unichar数组转换为单个NSString

[英]To convert Unichar Array into a single NSString

I created an unicode char array using 'characterAtIndex:' from an NSString object. 我从NSString对象使用'characterAtIndex:'创建了一个Unicode字符数组。

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? 有什么方法可以将这个unicahr数组转换成NSStirng吗? Can anyone please help me??? 谁能帮帮我吗???

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... 我找到了一种比这更好的反击方法,但是请让我知道我们是否有任何方法可以解决此问题。

You can convert to a unichar and back using getCharacters:range: and stringWithCharacters: . 您可以使用getCharacters:range:stringWithCharacters:转换为unichar并返回。

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);

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:遍历字符串)

Is the array a standard c array? 数组是标准的c数组吗? (looks something like this: array[50]; ) If it is, you can do this: (看起来像这样: array[50]; )如果是这样,则可以执行以下操作:

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

You can use Toll-Free Bridging to turn a char array into an objective-c class or reverse. 您可以使用免费电话桥接将char数组转换为Objective-C类或反向。

You can create a CFMutableStringRef variable with your array lets say the variable is myString. 您可以使用数组创建一个CFMutableStringRef变量,假设该变量为myString。 Than you can create a string or a mutable string with it. 比起您可以创建字符串或可变字符串。

NSMutableString* objcString = (NSMutableString*) myString;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM