aspose file tools*
The moose likes Beginning Java and the fly likes replaceAll() and backslash Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "replaceAll() and backslash" Watch "replaceAll() and backslash" New topic
Author

replaceAll() and backslash

eve agostini
Ranch Hand

Joined: Nov 01, 2005
Posts: 43
I just need to replace all the ' in a String whit \'
So i tried to use replaceAll()
but...
if I use:

it doesn't work. Ok I know, backslash is an escape character.
So i tried:

The same....
I tried also:

But it replaces the ' with \\': it puts two backslashes!!
I'm really stuck in this stupid problem!


Thanks
Eve

[ April 01, 2008: Message edited by: eve agostini ]

[ April 01, 2008: Message edited by: eve agostini ]
[ April 01, 2008: Message edited by: eve agostini ]
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19761
    
  20

I don't see why it would put two backslashes; text.replaceAll("'", "\\\\'") works perfectly for me.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Anubhav Anand
Ranch Hand

Joined: May 18, 2007
Posts: 341



This should work. Try this again.
eve agostini
Ranch Hand

Joined: Nov 01, 2005
Posts: 43
ok the mistery gets deeper.
You are right. I tried to write a very simple app and it actually works.
BUT...it still doesn't work in my code, which is called from a JSF bean. I really can't understand.
Anyway, since i wanted to use it just to prepare a mysql query, i solved the problem using PreparedStatement.
Anyway, thank you
Bill Shirley
Ranch Hand

Joined: Nov 08, 2007
Posts: 457
It's time to become familiar with your debugger and follow the execution to see what's happening that you're not expecting.


Bill Shirley - bshirley - frazerbilt.com
if (Posts < 30) you.read( JavaRanchFAQ);
eve agostini
Ranch Hand

Joined: Nov 01, 2005
Posts: 43
I obviously used the debugger...but I cannot see inside replaceAll() with the debugger...can I?
I just copy and past the 2 lines form my java app to my web app and it doesn't work anymore....String is contained in the basic packages...shouldn't be implemented in the same way if I use the same version of JDK?
Bill Shirley
Ranch Hand

Joined: Nov 08, 2007
Posts: 457
So, what's the complete code?
What's the enter and exit values?
Are you in Java or JavaScript?
Which JVM are you using?

This works just fine for me:

Bill Shirley
Ranch Hand

Joined: Nov 08, 2007
Posts: 457
hmm,

actually, (slightly modified)


it seems that my debugger (Eclipse) displays the single character '\' as \\ which definitely led to some confusion on my part,

though the code above does "pass" and outputs

\'something single quoted\'
eve agostini
Ranch Hand

Joined: Nov 01, 2005
Posts: 43
I'm using JVM 1.5.0 (from Apple) and JDK 1.5. I'm in Java. There is a jsp page where there is a html form. The values entered in the form are passed (by the JSF framework) to a backing bean, where they are processed; and my code is at that point. I cannot post the complete code since it's a large ammount of code. But I'll try to reproduce the problem with a simpler example.
Thanks for help
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: replaceAll() and backslash