Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replace " " character in an NSString with "\ " (to create a Unix path)

Using the following doesn't work:

- (NSString *)stringByReplacingOccurrencesOfString:(NSString *)target withString:(NSString *)replacement

By doesn't work I mean does not do any replacement what so ever. It returns the same exact string.

Is there a convenience method to do this? Similar to:

- (NSString *)stringByReplacingPercentEscapesUsingEncoding:(NSStringEncoding)encoding

My code (just one line):

NSString *escapedPath = [pathToBeConverted stringByReplacingOccurrencesOfString:@" " 
                                                                     withString:@"\ "];

Also, my compiler warning. Which likely has much to do with this:

warning: unknown escape sequence: '\040'
like image 398
Corey Floyd Avatar asked Oct 12 '09 21:10

Corey Floyd


1 Answers

You should use @"\\ " instead of @"\ ". In C \ is the escape character. You need to escape it with another \.

like image 145
mmx Avatar answered Nov 10 '22 07:11

mmx