Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

replaceAll() and backslash

 
eve agostini
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20495
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't see why it would put two backslashes; text.replaceAll("'", "\\\\'") works perfectly for me.
 
Anubhav Anand
Ranch Hand
Posts: 341
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


This should work. Try this again.
 
eve agostini
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 457
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's time to become familiar with your debugger and follow the execution to see what's happening that you're not expecting.
 
eve agostini
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 457
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 457
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic