问题描述
如何更改此
H:\ app \ new \ data \
像这样
H:\\ app \\ new \\ data \\
怎么做
我用这个,但是有一个错误
How to change this
H:\app\new\data\
Like this
H:\\app\\new\\data\\
How to do this
I use this but there is an error
string old = "H:\app\new\data\";
string new = Regex.Replace(old,"\","\\"); //Error
我是C#的新手
i''m new to C#
Thank in advance!
推荐答案
string old = @"H:\app\new\data\";
或:
string old = "H:\\app\\new\\data\\";
要删除编译器错误,然后(如果您确实需要这样做,并且我怀疑是否这样做:
To remove compiler errors, then (if you really need to do this, and I doubt if you do:
string new = Regex.Replace(old,@"\",@"\\");
Backslach是C#字符串中的特殊字符:它充当转义"字符,允许您输入空格值,例如
Backslach is a special character in C# string: it acts as an "escape" character to allow you to enter spacial values such as
double quote \"
backslash \\
newline \n
等.
当VS显示包含真正单反斜杠的字符串时,会将其显示为两个以表示它已被转义,而不是以下字符的一部分.
and so forth.
When VS shows strings containing a genuine single backslash, it shows it as two to indicate that it is escaped and not a part of the following character.
string oldString = @"H:\app\new\data\";
string newString = oldString.Replace(@"\",@"\\");
另请注意,new
是一个关键字,不能像在问题中那样用作变量名.
干杯
Andi
Please also note that new
is a keyword that you cannot use as variable name as you do in your question.
Cheers
Andi
这篇关于C#正则表达式替换字母的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!