Replace Double-Quote

  • Thread starter Thread starter Kevin Thomas
  • Start date Start date
K

Kevin Thomas

Hi there,

If I have a string var, strFoo that contains double-quotes such that it
looks like this: I "love" VB

What do I pass into the "replace" method to replace the double-quotes with
something else?

strFoo = strFoo.replace(???,""")

In other words, what would I pass in inplace of the ??? above to get rid of
any double quotes in the string var strFoo?

Thanks,

Kevin
 
Kevin Thomas said:
If I have a string var, strFoo that contains double-quotes such that it
looks like this: I "love" VB

What do I pass into the "replace" method to replace the double-quotes with
something else?

strFoo = strFoo.replace(???,""")

'... = strFoo.Replace("""", """)'.
 
You have to escape the " character, that's all.

The way to do that is replace instances of " with "".

Sample code -

Try
Dim str As String = "I ""love"" VB"
Console.WriteLine(str)
Console.WriteLine(str.Replace("""", ""))
Catch ex As Exception
Console.Write(ex.ToString())
Finally
Console.Read()
End Try
- Sahil Malik
You can reach me thru my blog at
http://www.dotnetjunkies.com/weblog/sahilmalik
 
Thanks everyone for your quick response.

The funny thing here is that I was escaping the " with """" but when viewing
my output in IE, the browser was itself changing the " back into " for
easy viewing, thus convincing me that it was imposible to replace a
double-quote. Once I viewed source in IE, I saw that the double-quote was
getting replaced.

Thanks again!

Kevin
 
Kevin,
In addition to the other comments, you can use also ControlChars.Quote.
strFoo = strFoo.replace(ControlChars.Quote,""")

However it appears that you are doing something with HTML or XML, rather
then attempting to use String.Replace to create well formed HTML or XML I
would use one of the Framework classes that already do this.

Such as:

HttpUtility.HtmlEncode & HttpUtility.HtmlDecode
HttpServerUtility.HtmlEncode & HttpServerUtility.HtmlDecode

XmlTextWriter & XmlTextReader

Depending on what you are really trying to do there may be other more
appropriate classes.

Hope this helps
Jay
 
Back
Top