【问题标题】:Why Replacing \" doest not work on my case?为什么替换 \" 不适用于我的情况?
【发布时间】:2019-03-10 00:11:45
【问题描述】:

我正在这样做,就像在这个 answer 中一样:

string test = "test\"test";
test = test.Replace("\\\"", "");

但结果仍然是test = "test\"test"

结果应该是test = "testtest",为什么我的替换不起作用?

【问题讨论】:

  • literal test\"test被编译器解析并存储为test"test,所以字符串中没有斜杠
  • 不确定是否标记为拼写错误...请注意\" 仅表示"

标签: c#


【解决方案1】:

因为您的字符串实际上是test"test 而不是test\"test。反斜杠用于转义双引号,它不在实际字符串中。

尝试使用逐字字符串:

string test = @"test\""test"; // equivalent to test\\\"test

【讨论】:

  • 逐字字符串并没有真正增加太多。无论哪种方式,您都必须逃避其中一个角色。如果没有逐字字符串,它将变为"test\\"test"。无论如何,你有这个问题的权利 - 转义的字符串被转义了:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-10-26
  • 2013-05-28
  • 1970-01-01
  • 2014-05-07
  • 1970-01-01
  • 2015-10-04
相关资源
最近更新 更多