C#正则表达式替换字母 [英] C# regex replace letter

查看:415
本文介绍了C#正则表达式替换字母的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何更改此

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!

推荐答案

这要归结于C#以及它如何处理带有反斜杠的字符串.试试这个:
That''s down to C# and how it handles strings with backslashes in them. Try this:
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.


您可能需要首先了解在C#中的转义. .看看我关于用C#进行转义的文章:字符,字符串,字符串格式,关键字,标识符 [ ^ ].

对于您的示例,不需要正则表达式,可以使用字符串. replace(...) [ ^ ],例如
You may need to understand escaping in C# first. Have a look at my article about Escaping in C#: characters, strings, string formats, keywords, identifiers[^].

For your example, you don''t need regex, you can use string.replace(...)[^], e.g.,
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


OriginalGriff为您提供了一个很好的答案.请注意,.NET还包括string.Replace函数,该函数可以将另一个字符(Type char)替换为另一个字符,也可以将字符串替换为字符串.

字符串s = @"\ 1 \ 2 \ 3 \ 4 \ 5 \ 6";

s = s.Replace(@"\",@"\\");

如果设置了一个断点并在执行替换操作后在运行时检查变量s,您将看到:

"\\\\ 1 \\\\ 2 \\\\ 3 \\\\\ 4 \\\\\ 5 \\\\\ 6"

但是,修改后的字符的长度将为#18.

您会注意到,调用string.Replace(字符串目标,字符串替换)会从其操作中返回 new 字符串.所有字符串运算符都会创建它们操作的字符串的副本,因为.NET中的字符串是不可变的.
OriginalGriff has provided you with a great answer. Note that .NET also includes the string.Replace function, which can replace either a single character (Type char) with another single character, or a string with a string.

string s = @"\1\2\3\4\5\6";

s = s.Replace(@"\", @"\\");

If you set a break-point and inspect the variable ''s at run-time after the Replace operation you''ll see:

"\\\\1\\\\2\\\\3\\\\4\\\\5\\\\6"

But, the length of the modified ''s will be #18.

You''ll note that calling string.Replace(string target, string replace) returns a new string from its operation. All string operators create copies of the string they manipulate because strings in .NET are immutable.


这篇关于C#正则表达式替换字母的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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