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

久未见 提交于 2019-12-23 08:57:14

问题


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'

回答1:


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



来源:https://stackoverflow.com/questions/1556979/replace-character-in-an-nsstring-with-to-create-a-unix-path

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!