c# - 替换 "\"的奇怪行为

标签 c#

我有这个问题,其中我有几次与文字表达式 "\\" 有冲突,我想用 "\" 替换它>,当我尝试用 string.replace 替换它时,只替换第一次出现的位置,如果我用正则表达式替换它,它根本不会替换它

我在网上与一些 RegEx 测试人员核对过,我的代码应该没问题,返回了我的意思,但我的代码根本不起作用

string.replace

example = "\\\\url.com\\place\\anotherplace\\extraplace\\";

example = example.replace("\\\\","\\");

returns example == "\\url.com\\place\\anotherplace\\extraplace\\";

使用正则表达式

example = Regex.Replace(example,"\\\\","\\");

returns example = "\\\\url.com\\place\\anotherplace\\extraplace\\";

如果我使用文字也是一样的情况(在 Replace 函数参数上使用 (@"\\", @"\") 给出与上面相同的结果)。

谢谢!

编辑:

我认为我的最终目标令人困惑所以我会在这里更新它,我想做的是:

输入: 保存字符串的变量:"\\\\url.com\\place\\anotherplace\\extraplace\\"

过程

输出 保存字符串 "\\url.com\place\anotherplace\extraplace\" 的变量 (这样我就可以将它发送到 ffmpeg 并将其识别为有效路由)

最佳答案

改变这个:

example = "\\\\url.com\\place\\anotherplace\\extraplace\\"; 

对此

example = @"\\\\url.com\\place\\anotherplace\\extraplace\\"; 

问题不在于 Regex.Replace 参数。

关于c# - 替换 "\"的奇怪行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12940313/

相关文章:

c# - 以类型安全的方式使用另一个实例的属性子集创建一个实例?

c# - 在 C# 中定义 extern "C"函数

c# - 如何从自定义 Windows Phone 8.1.app 调用系统摄像头

c# - 在 Xamarin 中获取 Android 联系人

c# - 局部变量范围错误 - foreach 循环

c# - 静态访问数据库=不好?

c# - 具有 new() 约束的泛型,不能将字符串作为类型

c# - 模数的偶数/奇数循环计数

c# - 在 Windows Phone 8.1 上捕获转储

C# 检查 Regex 是否匹配,返回 True 而不是 False