users
[Top] [All Lists]

Re: [cinjug-users] String problem???

To: Chris McMahan <cmcmahan@xxxxxxx>
Subject: Re: [cinjug-users] String problem???
From: Elijah Roberts <eroberts@xxxxxxxxxxxxxxxx>
Date: Wed, 11 Jun 2003 09:48:27 -0400
Cc: Ray <andern@xxxxxxxxxxxx>, users@xxxxxxxxxx
Delivered-to: mailing list users@cinjug.org
In-reply-to: <16103.12086.572000.449248@gargle.gargle.HOWL>
Mailing-list: contact users-help@cinjug.org; run by ezmlm
References: <003e01c3301a$a7839fd0$9865fea9@CodeMasters> <16103.12086.572000.449248@gargle.gargle.HOWL>
User-agent: Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.3) Gecko/20030312
String are unmodifiable in Java. What you really want is:

originalString = originalString.replaceAll("\"", "\\\"" );


Elijah


Chris McMahan wrote:

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>&nbsp;</DIV>
<DIV><FONT face=Arial size=2>I'm attempting to change all occurrences of double quotes ( " )&nbsp;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>&nbsp;</DIV>
<DIV><FONT face=Arial size=2>Ray</FONT></DIV>
<DIV><FONT face=Arial size=2>-------------------</FONT></DIV></BODY></HTML>








<Prev in Thread] Current Thread [Next in Thread>