2

I'm using String.Replace to replace certain characters. How do I replace the " sign with a different sign of my choice?

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

That doesn't seem to work

AzulShiva
  • 201
  • 2
  • 17
  • Or `Replace(@"""", "!");` – AgentFire Oct 09 '15 at 08:21
  • 2
    http://stackoverflow.com/questions/9393879/double-quote-string-replace-in-c-sharp http://stackoverflow.com/questions/24652063/c-sharp-replace-double-quotes-to-empty-string http://stackoverflow.com/questions/3905946/how-to-add-doublequotes-to-a-string-that-is-inside-a-variable http://stackoverflow.com/questions/9582781/c-sharp-two-double-quotes does this question shows any research effort? No. – M.kazem Akhgary Oct 09 '15 at 08:25
  • even google answers it https://www.google.com/search?q=how+to+replace+with+double+quotes+in+c%23&ie=utf-8&oe=utf-8 – M.kazem Akhgary Oct 09 '15 at 08:26
  • 1
    @M.kazemAkhgary take a look at [this one then](http://stackoverflow.com/questions/948135/how-can-i-write-a-switch-statement-in-ruby) :) – Thomas Ayoub Oct 09 '15 at 08:27
  • 1
    It's useful to know how to escape things, but you don't really need to here. You want a char by char substitution (i.e. replace `'"'` with `'!'`). – Asad Saeeduddin Oct 09 '15 at 08:29

6 Answers6

10

Either:

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

Or:

artikelBezeichnung = artikelBezeichnung.Replace(@"""", "!");
Jcl
  • 27,696
  • 5
  • 61
  • 92
5

Escaping isn't really necessary, since String.Replace has an overload that accepts a char:

artikelBezeichnung = artikelBezeichnung.Replace('"', '!');
Asad Saeeduddin
  • 46,193
  • 6
  • 90
  • 139
1

You have to add a backslash.

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

or adding the @ symbol

artikelBezeichnung = artikelBezeichnung.Replace(@"""", "!");
Galma88
  • 2,398
  • 6
  • 29
  • 50
1

The quote is an special char in C#. You need use it as an string literal, You need to scape it:

Using slash:

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

Or using @:

artikelBezeichnung = artikelBezeichnung.Replace(@"""", "!");
Community
  • 1
  • 1
Daniel Peñalba
  • 30,507
  • 32
  • 137
  • 219
1
string stringContainingContent = "fsdfsfsdfsdfsdfsdfsdfsf\"sdfsdfsdffsd";
            string test = "\"";
            string test1 = stringContainingContent.Replace(test, "!");

You can replace using Skip Sequence.

1

Using ASCII

artikelBezeichnung = artikelBezeichnung.Replace(Char.ConvertFromUtf32(34), "!");
Lewis Hai
  • 1,114
  • 10
  • 22