如何用反斜杠替换正斜杠

时间:2022-05-31 21:19:14

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

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

2 个解决方案

#1


34  

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

视觉工作室intellisense仍会显示“\\”,如果你将鼠标悬停在字符串上,你会找到一个放大镜,点击它。这将显示真正的字符串

#2


4  

Try escaping backslash,

尝试逃避反斜杠,

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

#1


34  

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

视觉工作室intellisense仍会显示“\\”,如果你将鼠标悬停在字符串上,你会找到一个放大镜,点击它。这将显示真正的字符串

#2


4  

Try escaping backslash,

尝试逃避反斜杠,

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