替换“ “,字符在NSString中具有“\” (创建Unix路径) [英] Replace " " character in an NSString with "\ " (to create a Unix path)

查看:163
本文介绍了替换“ “,字符在NSString中具有“\” (创建Unix路径)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用下列方法无效:

- (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'


推荐答案

您应该使用 @\\ / code>而不是 @\。在C \ 是转义字符。您需要使用另一个 \ 转义它。

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

这篇关于替换“ “,字符在NSString中具有“\” (创建Unix路径)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆