How to replace straight quotation mark (")

31,452

Solution 1

I agree with Heinzi, you should use " instead of &, and & means "&" Btw, after invoking the Replace method, don't forget to set the value to someWord again:

someWord = someWord.Replace("\"", """);

And there is another way to do it. Add the reference System.Web, and using System.Web; then:

someWord = HttpUtility.HtmlEncode(someWord);

Solution 2

someWord.Replace("\"", "&");

or

someWord.Replace(@"""", "&");

(Quotes are escaped as \" in regular strings and "" in verbatim strings.)

But you probably meant

someWord.Replace("\"", """);

since the HTML entity for straight quotation marks is ", not &.

Solution 3

someWord.Replace(@"""", "&");

or

someWord.Replace("\"", "&");
Share:
31,452

Related videos on Youtube

Willem
Author by

Willem

.net Fan Love wpf MVVM + M

Updated on October 28, 2020

Comments

  • Willem
    Willem over 3 years

    I would like to replace a straight quotation mark (") using C#.

    I might be missing something small, but I can't get it with a normal string.Replace();

    someWord.Replace(@""", "&");

    Can I do it with a normal string.Replace(); or do I need to use Regex? If the latter, what would the Regex replace look like?

  • Willem
    Willem over 12 years
    Will this replace all special characters? (HttpUtility.HtmlEncode(someWord);)
  • ojlovecd
    ojlovecd over 12 years
    @Willem Yes,it will. If you just want to replace the straight quotation mark, don't use it.
  • Willem
    Willem over 12 years
    Thats great. Will save me alot of time. Thanks