如何替换“与\“在字符串生成器中 [英] How to replace " with \" in string builder

查看:71
本文介绍了如何替换“与\“在字符串生成器中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我正在使用字符串生成器,我想用"\"替换任何人都可以帮助我...


例如:

字符串ex =测试总是"总是很好";

我需要输出,因为:测试始终是很好"

在此先感谢...

Hi,

I am using string builder, I want to replace " with \" can anyone help me out...


Ex:

String ex="testing "is" always good";

I Need output as : testing is \" is \" always good

Thanks in advance...

推荐答案

尝试类似的方法:
Try something like:
StringBuilder sb = new StringBuilder();
// to keep double quotes in it, it was escaped by '\'
sb.Append("Some text in here with \"this\" in double quotes");
sb.Replace(@'"', @'\"');




更新:
根据更新后的问题,OP需要:




UPDATE:
Based on the updated question, OP needs:

String ex="testing "is" always good";
//I Need output as : testing is \" is \" always good



为此,听起来,他想在这里再加一个反斜杠.不知道为什么,现在我不问了,所以尝试:



For this, it sounds like, he wants to have a extra back slash here. No idea why and now I am not going to ask, so try:

StringBuilder sb = new StringBuilder();
// to keep double quotes in it, it was escaped by '\'
sb.Append("Some text in here with \"this\" in double quotes");
sb.Replace(@'"', @'\\"');


是这样,您可以将其放入数据库中吗?如果是这样,则您正在执行他们所谓的转义"字符,您需要执行以下操作:

Is this so you can put it into a database? If so, you''re doing what they call "escaping" a character, and you need to do this:

StringBuilder sb = new StringBuilder("This is a ""test"" string");
sb.Replace("\"", "\"\"");



如果没有,我想不出您想要这样做的任何原因,但是您需要这样做:



If not, I can''t think of any reason you''d want to do this, but you need to do this:

StringBuilder sb = new StringBuilder("This is a ""test"" string");
sb.Replace("\"", "\\\"");


这篇关于如何替换“与\“在字符串生成器中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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