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?
-
How do you create this string? Could you paste some code?– xxcvSep 19, 2011 at 13:21
-
1I am not created but google place api gives me responce that string so I want to use my apps with proper format– Nikunj JadavSep 19, 2011 at 13:22
-
possible duplicate of How can I escape unicode characters in a NSString?– AbizernSep 19, 2011 at 13:23
-
Are you receiving this as an NSString or NSData or characters?– zaphSep 19, 2011 at 13:53
-
If you are receiving NSData or a C string please do an NSLog on NSData, that will be the actual hex received. NSString is not really showing the data but a representations. Please post the NSData hex.– zaphSep 19, 2011 at 17:19
3 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'
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
You can use it in following way.
NSString *localStr = @"L\U00c3\U00b6wenbr\U00c3\U00a4u Keller";
localStr = [localStr stringByReplacingOccurrencesOfString:@"'" withString:@"'"];
localStr = [localStr stringByReplacingOccurrencesOfString:@" " withString:@" "];
localStr = [localStr stringByReplacingOccurrencesOfString:@""" withString:@"'"];
localStr = [localStr stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
localStr = [localStr stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
I hope it will be helpful to you and will display exact string.
Cheers.
-
-
Whatever data it is return, store that data inside "localStr" variable. Then put the remaining code. And at last check "localStr" with NSLog. Let me know in case of any difficulty. Sep 19, 2011 at 14:26