开发者

NSString format problem

I am working with the Google Place API and got a successful JSON response. But one NSString开发者_如何学C 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?


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜