Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSString format problem

I am working with the Google Place API and got a successful JSON response. But one NSString is L\U00c3\U00b6wenbr\U00c3\U00a4u Keller. I want to convert it into a proper NSString like Lowenbrau Keller. How can I do this conversion?

like image 821
Nikunj Jadav Avatar asked Sep 19 '11 13:09

Nikunj Jadav


2 Answers

The correct format is to use a lowercase u to denote unicode in Coocoa:

Wrong:

NSString *string1 = @"L\U00c3\U00b6wenbr\U00c3\U00a4u Keller";

Correct:

NSString *string2 = @"L\u00c3\u00b6wenbr\u00c3\u00a4u Keller";

To get it to print correctly replace \u00 with \x

NSString *string3 = @"L\xc3\xb6wenbr\xc3\xa4u Keller";
NSLog(@"string3: '%@'", string4);

NSLog output: string3: 'Löwenbräu Keller'

like image 95
zaph Avatar answered Nov 05 '22 05:11

zaph


TESTED CODE:100 % WORKS

NOTE:

\U and \u are not the same thing. The \U escape expects 8 (hex) digits instead of 4.

NSString *inputString =@"L\u00c3\u00b6wenbr\u00c3\u00a4u Keller";



NSString *outputString=[[NSString stringWithFormat:@"%@",inputString] stringByFoldingWithOptions:NSDiacriticInsensitiveSearch locale:[NSLocale currentLocale]];

NSLog(@"outputString : %@ \n\n",outputString);

OUTPUT:

outputString : LA¶wenbrA¤u Keller
like image 1
Vijay-Apple-Dev.blogspot.com Avatar answered Nov 05 '22 06:11

Vijay-Apple-Dev.blogspot.com