简体   繁体   English

C#:字符串中的转义点字符

[英]C#: escape dot character in string

I need to replace all ocurrences of a dot with a backslash in C# with the string "\\." 我需要将C#中的所有点都用反斜杠替换为字符串“ \\”。 (escape the dot), (转义点),

Wath I've tried: 我尝试过的方式:

string.Replace(".", "\\.");
string.Replace(".", @"\.");
string.Replace(".", @"\\\.");

all my tries finish with an even number of backslashes before the dot, that are removed automatically after. 我所有的尝试都以小数点前的偶数个反斜杠结束,此后将自动删除。

Thanks 谢谢

The first two both work perfectly! 前两个都完美地工作!

You're probably watching the values with the Watch function from a breakpoint, but this shows backslashes with additional escapes. 您可能正在从断点使用Watch函数监视值,但是这会显示反斜杠以及其他转义符。

If you just output the string to something like the console or a file, you'll see that it works. 如果仅将字符串输出到控制台或文件之类的文件,则会看到它可以正常工作。

为此设计了一种方法:Regex.Escape()。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM