【问题标题】:C# Putting a quotation mark within quotation marks?C#在引号内加引号?
【发布时间】:2016-01-07 02:38:19
【问题描述】:

我正在使用 String.Replace 替换某些字符。如何用我选择的其他符号替换 " 符号?

artikelBezeichnung = artikelBezeichnung.Replace(""", "!");

这似乎不起作用

【问题讨论】:

标签: c#


【解决方案1】:

要么:

artikelBezeichnung = artikelBezeichnung.Replace("\"", "!");

或者:

artikelBezeichnung = artikelBezeichnung.Replace(@"""", "!");

【讨论】:

  • 你的意思是Replace(@""", "!"); 吗?
  • @Thomas 不,在逐字字符串中,插入引号将它们加倍,所以@""""
  • 感谢大家,不幸的是,谷歌不会搜索像“”这样的特殊符号
  • @"""" 确实,只有 3x " 使事情变得一团糟;) tyvmn
【解决方案2】:

转义并不是真正必要的,因为String.Replace has an overload 接受char

artikelBezeichnung = artikelBezeichnung.Replace('"', '!');

【讨论】:

    【解决方案3】:

    你必须添加一个反斜杠。

    artikelBezeichnung = artikelBezeichnung.Replace("\"", "!");
    

    或添加@符号

    artikelBezeichnung = artikelBezeichnung.Replace(@"""", "!");
    

    【讨论】:

      【解决方案4】:

      引号是 C# 中的一个特殊字符。您需要将其用作string literal,您需要将其用作scape it

      使用斜线:

      artikelBezeichnung = artikelBezeichnung.Replace("\"", "!");
      

      或者使用@:

      artikelBezeichnung = artikelBezeichnung.Replace(@"""", "!");
      

      【讨论】:

      • 与@Jcl 答案相同
      【解决方案5】:
      string stringContainingContent = "fsdfsfsdfsdfsdfsdfsdfsf\"sdfsdfsdffsd";
                  string test = "\"";
                  string test1 = stringContainingContent.Replace(test, "!");
      

      您可以使用跳过序列替换。

      【讨论】:

        【解决方案6】:

        使用 ASCII

        artikelBezeichnung = artikelBezeichnung.Replace(Char.ConvertFromUtf32(34), "!");
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2014-01-22
          • 2013-11-19
          • 2013-09-24
          • 1970-01-01
          • 2012-02-21
          • 1970-01-01
          • 1970-01-01
          • 2014-09-01
          相关资源
          最近更新 更多