“与\'在C#替换 [英] Replace ' with \' in C#

查看:75
本文介绍了“与\'在C#替换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这个变量,我想每前增加一些\。

In this variable, i would like to add some \ before every '.

string html = 
    "<a href=\"annee-prochaine.html\">Calendrier de l'annee prochaine</a>"

html = html.Replace("'", "\'"); //No change
html = html.Replace("\'", "\'"); //No change

html = html.Replace("\'", "\\'");
//html => <a href=\"annee-prochaine.html\">Calendrier de l\\'annee prochaine</a>
html = html.Replace("\'", @"\'");
//html => <a href=\"annee-prochaine.html\">Calendrier de l\\'annee prochaine</a>



我想获得该更换后:

I would like to get that after Replace :

//html => <a href=\"annee-prochaine.html\">Calendrier de l\'annee prochaine</a>



任何想法?

Any ideas ?

谢谢!

推荐答案

我强烈怀疑,你看在调试器,这就是为什么你看到了一倍反斜杠的字符串。

I strongly suspect that you're looking at the strings in the debugger, which is why you're seeing doubled backslashes.

这版本是绝对的罚款:

html = html.Replace("\'", "\\'");



而不是看它(使用逐字字符串将被罚款了。在一个)调试器,记录它,还是服务于它,一切都应该罚款。

(The one using the verbatim string literal would be fine too.) Rather than looking at it in the debugger, log it or just serve it, and everything should be fine.

这是你看到它的事实双引号也就是这进一步证明。例如,下面的字符串:

The fact that you're seeing it for the double-quote as well is further evidence of this. For example, this string:

string html = "<a href=\"anne...";

...做的的包含反斜杠,但是你的诊断都出现了,这就是我在调试器的期望。

... does not contain a backslash, but your diagnostics are showing it, which is what I'd expect in a debugger.

这篇关于“与\'在C#替换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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