Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

stringByReplacingOccurrence does not replace string

I need to parse a FILE URL in my application, and replace the %20 for a SPACE. I am using stringByReplacingOccurance:

NSString *strippedContent = [finalFilePath stringByReplacingOccurrencesOfString:@"%20" withString:@" "];

But when I display strippedContent in an NSLog, all of the %20 strings are still there. Here is an example of the file name I hope to parse:

.../Documents/Inbox/Test%20Doc%20From%20Another%20App.txt

It seems as if NSFileManager cannot find the document when it has the %20 in it. The file path is being passed from another application through the "Open In..." dialogue. Is there any way to remove the %20 with stringByReplacingOccurrence or when the URL is imported?

like image 364
Sam Spencer Avatar asked Aug 10 '26 05:08

Sam Spencer


2 Answers

NSString provides a method that performs the conversion that you need:

NSString *strippedContent = [finalFilePath stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
like image 77
Sergey Kalinichenko Avatar answered Aug 11 '26 20:08

Sergey Kalinichenko


Yes you should use:

NSString * strippedContent = [finalFilePath stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
like image 33
self Avatar answered Aug 11 '26 21:08

self