xcode 如何在我的整个 xml 字符串中将双引号转义为 \"?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9107232/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 23:11:10  来源:igfitidea点击:

How do I escape double quotes to \" in my entire xml string?

iphoneobjective-ciosxmlxcode

提问by TeaCupApp

Very simple question but giving me hard time, I want to replace " to \" in my entire xml file. I tried but don't know how to do it.

很简单的问题,但让我很难,我想在我的整个 xml 文件中替换“到\”。我试过了,但不知道该怎么做。

My try :

我的尝试:

escapedXMLString = [xml stringByReplacingOccurrencesOfString:@""" withString:@"\""];

of course above line won't even compile, as I am escaping the double quote. So is there any work around?

当然上面的行甚至不会编译,因为我正在逃避双引号。那么有什么解决办法吗?

回答by MobileOverlord

You need the \ to escape the " in the string for the find and a \ for the \" in the replace.

您需要使用 \ 来转义字符串中的 " 以进行查找,并使用 \ 来转义替换中的 \"。

escapedXMLString = [xml stringByReplacingOccurrencesOfString:@"\"" withString:@"\\""];

回答by Chaitanya Gupta

Try [xml stringByReplacingOccurrencesOfString:@"\"" withString:@"\\\""]);

尝试 [xml stringByReplacingOccurrencesOfString:@"\"" withString:@"\\\""]);

回答by diadyne

If you need to generate this literal for something more involved you might consider using a tool like this:

如果您需要为更多涉及的内容生成此文字,您可以考虑使用这样的工具:

http://www.freeformatter.com/java-dotnet-escape.html#ad-output

http://www.freeformatter.com/java-dotnet-escape.html#ad-output

to accomplish the work for you.

为您完成工作。

See also this answer.

另请参阅此答案