简体   繁体   中英

C#: escape dot character in string

I need to replace all ocurrences of a dot with a backslash in C# with the string "\\." (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.

If you just output the string to something like the console or a file, you'll see that it works.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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