Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 在ios中替换字符串_Objective C_String_Replace - Fatal编程技术网

Objective c 在ios中替换字符串

Objective c 在ios中替换字符串,objective-c,string,replace,Objective C,String,Replace,在objective-c中,我想将字符串“AB\(cd)替换为“AB\(cd)。这是我尝试过的 NSMutableString * str = @"AB\(cd"; NSString * replace = @"\\("; [str stringByReplacingOccurrencesOfString:@"\(" withString:replace]; NSLog(@"%@",str); 结果是“AB(cd”

在objective-c中,我想将字符串
“AB\(cd)
替换为
“AB\(cd)
。这是我尝试过的

NSMutableString * str = @"AB\(cd";
NSString * replace = @"\\(";
[str stringByReplacingOccurrencesOfString:@"\("
                                       withString:replace];

NSLog(@"%@",str);
结果是
“AB(cd”
,但我希望它是
“AB\\\(cd”


有人能帮我一下吗

您忘了从
[NSString stringByReplacingOccurrencesOfString:with string:
:

NSString *replaced = [str stringByReplacingOccurrencesOfString:@"\\("
                                                    withString:@"\\\\("];
但是,如果要替换原始的
str
,则需要使用可变副本重新分配它,因为它是
NSMutableString

str = [str stringByReplacingOccurrencesOfString:@"\\("
                                     withString:@"\\\\("] mutableCopy];

我希望它以AB双反斜杠的形式出现(cd@user3115014是的,这就是我答案中的代码应该做的。如果我把它放进它的修剪部分slash@user3115014啊,我明白了;我已经修改了答案中的代码。@Popeye,我相信你已经用编辑更改了问题的全部含义。@user1190882以什么方式?你不能用
iOS
替换字符串,你可以用code(在本例中为Objective-c)替换字符串,因此我已正确地替换了该字符串。此问题的代码没有意义,因为它是
stringByReplacingOccurrencesOfString:withString:
返回一个字符串,而您不会将其传递到
str
中,因此
str
应保持为
“AB\(cd)”
当记录它时。更不用说编译器不喜欢字符串文字中的
\(
。@Popeye,在开始时,我可以将其视为
“AB\\(CD”
,在结束时,我可以将其视为
“AB\\(CD)