通过此方法stringByAddingPercentEncodingWithAllowedCharacters更改NSURL字符串

时间:2015-09-07 07:41:17

标签: ios objective-c nsstring nsurl

我有一个字符串

https://www.example.com/abc/Gesture-abc-mobile-app_%257E0177cae9d41f573e5f?source=rss

当我使用此方法在Url中对其进行转换以便在UIWebView

中加载时
 NSString *str = [[NSString stringWithFormat:@"%@", self.urlStr] stringByAddingPercentEncodingWithAllowedCharacters:NSCharacterSet.URLQueryAllowedCharacterSet];

输出是:

 https://www.example.com/jobs/Gesture-initiated-mobile-app_%257E0177cae9d41f573e5f?source=rss%0A%20%20%20%20%20%20

告诉我为什么会这样......

1 个答案:

答案 0 :(得分:0)

字符串末尾有多余的字符 - 回车符和确切的空格:

NSString *urlStr = @"https://www.example.com/abc/Gesture-abc-mobile-app_%257E0177cae9d41f573e5f?source=rss\n     ";

<强>结果

https://www.example.com/abc/Gesture-abc-mobile-app_%25257E0177cae9d41f573e5f?source=rss%0A%20%20%20%20%20

删除末尾的垃圾,你会得到所需的结果。