Nikunj Jadav
Nikunj Jadav

Reputation: 3402

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?

Upvotes: 9

Views: 1074

Answers (3)

zaph
zaph

Reputation: 112865

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'

Upvotes: 4

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

Upvotes: 1

Nishant B
Nishant B

Reputation: 2907

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.

Upvotes: 0

Related Questions