users
[Top] [All Lists]

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

To: Chris McMahan <cmcmahan@xxxxxxx>
Subject: Re: [cinjug-users] String problem???
From: "Jason R. Kretzer" <jason@xxxxxxxxxxxxxx>
Date: 11 Jun 2003 09:46:24 -0400
Cc: 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
Organization: OpinionOne
References: <003e01c3301a$a7839fd0$9865fea9@CodeMasters> <16103.12086.572000.449248@gargle.gargle.HOWL>
If the replaceAll function is giving you problems try something similar to this.
private String fixString(String str)
{
    StringBuffer sb = new StringBuffer(str);

    for (int g2 = 0; g2 < sb.length(); g2++)
    {
        if (sb.charAt(g2) == '\"')
        {
            sb.insert(g2, '\\');
            g2++;//do this so you won't get an infinite loop
        }
   }
    return sb.toString();
}
I think this works.

-Jason


On Wed, 2003-06-11 at 09:31, 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>