在c#中用单个斜杠替换双向斜杠 [英] Replacing double backwards slashes with single ones in c#

查看:751
本文介绍了在c#中用单个斜杠替换双向斜杠的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要用单引号替换双引号,以便像这样的东西

I need to replace double quotes with single so that something like this

\\\\servername\\dir1\\subdir1\\

成为

\\servername\dir1\subdir1\

我尝试过这个

string dir = "\\\\servername\\dir1\\subdir1\\";
string s = dir.Replace(@"\\", @"\"); 

我得到的结果是

\\servername\\dir1\\subdir1\\

任何想法?

推荐答案

你不需要在这里替换任何东西。反斜杠被转义,这就是为什么它们翻倍。

就像 \t 表示一个制表符, \\ 代表一个 \ 。您可以看到 Escape Sequences的完整列表 MSDN

You don't need to replace anything here. The backslashes are escaped, that's why they are doubled.
Just like \t represents a tabulator, \\ represents a single \. You can see the full list of Escape Sequences on MSDN.

string dir = "\\\\servername\\dir1\\subdir1\\";
Console.WriteLine(dir);

这将输出 \\servername\dir1\subdir1\\ \\

BTW:您可以使用逐字字符串使其更易读:

BTW: You can use the verbatim string to make it more readable:

string dir = @"\\servername\dir1\subdir1\";

这篇关于在c#中用单个斜杠替换双向斜杠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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