File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes escaping single quotes in a java.lang.String Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "escaping single quotes in a java.lang.String " Watch "escaping single quotes in a java.lang.String " New topic

escaping single quotes in a java.lang.String

Gabriel Fox
Ranch Hand

Joined: Oct 17, 2001
Posts: 170
Hi all,
I have a search screen, which searches using firstname and lastname.
It goes over to the server to run a query using either input.
But, for a name like : O'neil (i.e. with single quote), my application
falls over.
Please, how do i escape single quote in a java.lang.String ....
Code will also be appreciated
[ November 11, 2003: Message edited by: Gabriel Fox ]
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
I'd guess there's a good chance Java is having no problems but the database is because it uses single quotes around literal strings. So you need to find out what the database wants to escape single quotes. Maybe two of them in a row? Then look at String.replaceAll() if you're in JDK 1.4 or later. If you have an older JDK you may have to write your own replaceAll().

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
I agree. Here's the link:
subject: escaping single quotes in a java.lang.String
It's not a secret anymore!