At first I thought of the obvious answer of replacing the \" with a
\\\", but on compiling a test program, that didn't work.
Looking at the javadoc, String.replace takes to chars, not strings, so
concatenating the \" wouldn't work here.
I tried replaceAll, which takes a String regex, String replacement
Here's the test:
public class TestString {
public TestString() {
} // TestString constructor
public static void main(String[] args) {
String originalString = "Original \"string\"";
System.out.println(originalString);
originalString.replaceAll("\"", "\\\"" );
System.out.println(originalString);
} // end of main()
} // TestString
It comiles and runs just fine, and according to the regexp rules
should output what you want, but the output looks like this:
Original "string"
Original "string"
This could be an artifact of the \ being interpreted as another escape
character within the System.out.println.
Of course, the test could be inaccurate because I had to escape the "
character in the originalString definition as well.
It's getting a bit more interesting now! :)
- Chris
Ray writes:
>Hi folks,
>
>I'm attempting to change all occurrences of double quotes ( " ) in a string to
>slash double quotes ( \" ). What's the best way to do this? string.replace(' "
>' , ' \" ') does not work. Any ideas?
>
>Ray
>-------------------
><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
><HTML><HEAD>
><META http-equiv=Content-Type content="text/html; charset=iso-8859-1">
><META content="MSHTML 6.00.2716.2200" name=GENERATOR>
><STYLE></STYLE>
></HEAD>
><BODY bgColor=#ffffff>
><DIV><FONT face=Arial size=2>Hi folks,</FONT></DIV>
><DIV><FONT face=Arial size=2></FONT> </DIV>
><DIV><FONT face=Arial size=2>I'm attempting to change all occurrences of
>double
>quotes ( " ) in a string to slash double quotes ( \" ). What's the best
>way
>to do this? string.replace(' " ' , ' \" ') does not work. Any
>ideas?</FONT></DIV>
><DIV><FONT face=Arial size=2></FONT> </DIV>
><DIV><FONT face=Arial size=2>Ray</FONT></DIV>
><DIV><FONT face=Arial size=2>-------------------</FONT></DIV></BODY></HTML>
--
================================
Chris McMahan | cmcmahan@xxxxxxx
================================
|