Replace " char with empty.string

inkedGFX

Well-known member
Joined
Feb 2, 2013
Messages
142
Programming Experience
Beginner
I am trying to replace the " symbol from a string with string.Replace()

I'm having some trouble getting the replace to work due to the char I want to replace.....
anyone know how to do this?

any help is appreciated
-InkedGFX
 

jmcilhinney

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
3,272
Location
Sydney, Australia
Programming Experience
10+
In C# you handle a special character in a string by escaping it with a backslash, e.g. adding a carriage return and line feed using "\r\n". Because a string literal is delimited by double quotes, a double quote is considered a special character so you escape it the same way, e.g. "He said \"Hello\" to me". A char literal is delimited by single quotes though, so a double quote is not a special character in that case. As such, a double quote char literal is simply '"'.

Because an empty string is a string and not a char, you must use the overload of string.Replace that replaces a string with a string rather than the one that replaces a char with a char. As such, you need a string that contains a double quote. Based on my first paragraph, that gives you two choices:
// Using a string literal with an escape character.
str1 = str1.Replace("\"", string.Empty);

// Using a char literal.
str2 = str2.Replace('"'.ToString(), string.Empty);
 
Top Bottom