I created an unicode char array using 'characterAtIndex:' from an NSString object.
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???
This s my code:
NSString *sentence = @"A long string";
N开发者_如何学编程SString *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:
.
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);
Is the array a standard c array? (looks something like this: array[50];
) If it is, you can do this:
[NSString stringWithCharacters:(const unichar*) length:(NSUInteger)];
Get a NSString
from a bytes array with:
- (id)initWithBytes:(const void *)bytes length:(NSUInteger)length encoding:(NSStringEncoding)encoding
(and you could use UTF8String
instead of looping through the string with characterAtIndex:
)
You can use Toll-Free Bridging to turn a char array into an objective-c class or reverse.
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.
NSMutableString* objcString = (NSMutableString*) myString;
精彩评论