ios 来自nsstring的ios UTF8编码

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11786075/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 19:34:13  来源:igfitidea点击:

ios UTF8 encoding from nsstring

iphoneobjective-ciosxcodecocoa-touch

提问by Jaume

I am receiving a nsstring that is not properly encoded like "mystring%201, where must be "mystring 1". How could I replace all characters that could be interpreted as UTF8? I read a lot of posts but not a full solution. Please note that nsstring is already encoded wrong and I am not asking about how to encode char sequence. Thank you.

我收到了一个未正确编码的 nsstring,如“mystring%201,其中必须是“mystring 1”。如何替换所有可以解释为 UTF8 的字符?我阅读了很多帖子,但不是完整的解决方案。请请注意,nsstring 已经编码错误,我不是在问如何编码字符序列。谢谢。

回答by Jesse Naugher

- (NSString *)stringByReplacingPercentEscapesUsingEncoding:(NSStringEncoding)encodingis what you want. basically use it like so:

- (NSString *)stringByReplacingPercentEscapesUsingEncoding:(NSStringEncoding)encoding是你想要的。基本上像这样使用它:

newString = [myString stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

回答by Osama Khalifa

[urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]

回答by Mehul

lblDate.text=[NSString stringWithCString:[[arrTripDetail valueForKey:Param_vCurrencySymbol] UTF8String] encoding:NSUTF8StringEncoding];

U can Convert & get Custom Emoji to string

你可以将自定义表情符号转换为字符串

eg :

例如:

input : \U20b9

Output: ?

输入:\U20b9

输出: ?

回答by A-Live

Check the Strings and Non-ASCII Characterssection of Formatting String Objects:

检查Strings and Non-ASCII Characters的部分格式化字符串对象

NSString *s = [NSString stringWithUTF8String:"Long \xe2\x80\x94   dash"];

回答by Michael

Do you want just percent encoding/decoding or full URL encoding/decoding? -(NSString*)stringByReplacingPercentEscapesUsingEncoding: will work if it is just percent encoding, but if there is full URL encoding there (so, for example a space could be either %20 or +) then you'll need something like the url decoding in the three20 library (or search on there, there are lots of examples of how to do it such as URL decoding/encoding NSString).

您只需要百分比编码/解码还是完整的 URL 编码/解码?-(NSString*)stringByReplacingPercentEscapesUsingEncoding: 如果它只是百分比编码,它将起作用,但是如果那里有完整的 URL 编码(因此,例如一个空格可以是 %20 或 +),那么你将需要类似 url 解码的东西Three20 库(或在那里搜索,有很多示例说明如何执行此操作,例如URL 解码/编码 NSString)。