我需要在我的应用程序中解析一个文件 URL,并将 %20 替换为一个空格。我正在使用 stringByReplacingOccurance:
NSString *strippedContent = [finalFilePath stringByReplacingOccurrencesOfString:@"%20" withString:@" "];
但是当我在 NSLog 中显示 strippedContent 时,所有 %20 字符串仍然存在。这是我希望解析的文件名示例:
.../Documents/Inbox/Test%20Doc%20From%20Another%20App.txt
当 NSFileManager 中有 %20 时,它似乎找不到文档。 文件路径是从另一个应用程序通过“打开方式...”对话框传递的。有什么方法可以使用 stringByReplacingOccurrence 或导入 URL 时删除 %20 吗?
最佳答案
NSString
提供了一种执行您需要的转换的方法:
NSString *strippedContent = [finalFilePath stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
关于objective-c - stringByReplacingOccurrence 不替换字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10853364/