C# 如何用反斜杠替换正斜杠

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18278892/
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-08-10 11:47:19  来源:igfitidea点击:

How to replace forward slash with backward slash

c#.netstring

提问by vinay teja reddy

i have a string /Images/Me.jpgi want to replace forward slashes with backward slashes like this \Images\Me.jpg, iam using string.Replace("/","\"); but the output is \\Images\\Me.jpgplease help

我有一个字符串,/Images/Me.jpg我想用这样的反斜杠替换正斜杠,我\Images\Me.jpg使用 string.Replace("/","\"); 但输出是\\Images\\Me.jpg请帮忙

采纳答案by Sayse

you need to escape the slashes

你需要逃避斜线

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

Visual studios intellisense will still show "\\", if you hover over the string, you will find a magnifying glass, click it. This will show the real string

Visual Studios intellisense 仍然会显示“\\”,如果你将鼠标悬停在字符串上,你会发现一个放大镜,点击它。这将显示真正的字符串

回答by Optimus Prime

Try escaping backslash,

尝试转义反斜杠,

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