NSString格式问题

发布于 2024-12-05 13:55:21 字数 205 浏览 0 评论 0原文

我正在使用 Google Place API 并获得了成功的 JSON 响应。但是一个 NSStringL\U00c3\U00b6wenbr\U00c3\U00a4u Keller。我想将其转换为正确的 NSString,例如 Lowenbrau Keller。我怎样才能进行这种转换?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

抱猫软卧 2024-12-12 13:55:21

正确的格式是在 Coocoa 中使用小写的 u 来表示 unicode:

错误:

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

正确:

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

要使其正确打印,请将 \u00 替换为 \x

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

NSLog 输出: string3: 'Löwenbräu Keller'

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'

爱要勇敢去追 2024-12-12 13:55:21

测试代码:100 % 有效

注意:

\U 和 \u 不是同一件事。 \U 转义符需要 8 个(十六进制)数字而不是 4 个。

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



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

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

输出:

outputString : LA¶wenbrA¤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
自此以后,行同陌路 2024-12-12 13:55:21

您可以通过以下方式使用它。

NSString *localStr = @"L\U00c3\U00b6wenbr\U00c3\U00a4u 凯勒";

localStr = [localStr stringByReplacingOccurrencesOfString:@"'" withString:@"'"];
localStr = [localStr stringByReplacingOccurrencesOfString:@" " withString:@" "];
localStr = [localStr stringByReplacingOccurrencesOfString:@""" withString:@"'"];
localStr = [localStr stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
localStr = [localStr stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];

我希望它对您有所帮助,并且会显示准确的字符串。

干杯。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文