Convert Unicode characters to NSString

I got a string from webservice that contains a Unicode character. I want to convert this to a regular NSString. so how can i do this?

ex: "This is not your bike"

So how to remove unicode and replace it with its special character charactered.

The output will be: "This is not your bike."

+4
source share
3 answers
char cString[] = "This isn\u2019t your bike"; NSData *data = [NSData dataWithBytes:cString length:strlen(cString)]; NSString *string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; NSLog(@"result string: %@", string); 

That should work.

UPDATE FOR COMMENT:

The unicode character you specified is not supported in all fonts.

http://www.fileformat.info/info/unicode/char/92/fontsupport.htm

But it does.

http://www.fileformat.info/info/unicode/char/2019/fontsupport.htm

That is why it throws an error.

+7
source
 NSString *final_url = [NSString stringWithFormat:url]; final_url = [final_url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:final_url] cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:120.0]; NSURLResponse *response; NSError *error = [[NSError alloc] init]; NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error]; NSString *strResponse = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; SBJSON *objJSON = [SBJSON new]; NSMutableDictionary *objDataDic = [objJSON objectWithString:strResponse error:nil]; 
+2
source

There is a library that converts https://github.com/alexaubry/HTMLString . It converts all Unicode characters.

 let escapedSnack = "Fish & Chips" let snack = escapedSnack.removingHTMLEntities // "Fish & Chips" 
0
source

All Articles