如何删除/解码URL百分比编码? [英] How do I remove/decode URL percent encoding?
问题描述
我想获取一个 url
并将其转换为更具可读性的格式.例如,我有以下链接:
I want to take a url
and convert it to a more readable format. For example I have the following link:
http://en.wikipedia.org/wiki/S%C3%A1ndor_Font
我拿走了不必要的部分,剩下"S%C3%A1ndor_Font"
作为 NSString
.有什么方法可以将其转换为Sándor字体"
(实际上应该是什么),而不必键入特殊字符的每个单个组合并替换字符串的每个部分?
I take away the unnecessary parts and am left with "S%C3%A1ndor_Font"
as a NSString
. Is there any sort of way to convert this into "Sándor Font"
(what it actually should be), without having to type out every single combination of special characters and replacing each part of the string?
为演示如何使用此代码,我编写了以下示例代码:
To demonstrate how I want to use this, I wrote the following sample code:
//request is a NSURLRequest with a url of http://en.wikipedia.org/wiki/S%C3%A1ndor_Font
NSRange range = [[request.URL absoluteString] rangeOfString:@"/wiki/"];
NSString *substring = [[[request.URL absoluteString] substringFromIndex:NSMaxRange(range)] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
//ArticleTitleLabel is a UILabel
self.ArticleTitleLabel.text = substring;
最后,我希望标签说的是SándorFont"
,而不是"S%C3%A1ndor_Font"
.谢谢!
In the end I want the label to say "Sándor Font"
not "S%C3%A1ndor_Font"
. Thanks!
推荐答案
-(NSString *)stringByReplacingPercentEscapesUsingEncoding:(NSStringEncoding)encoding;
是您想要的.
即
[substring stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
这篇关于如何删除/解码URL百分比编码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!