Logo 
Search:

Java Answers

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds
  Question Asked By: Abelerd Fischer   on Mar 28 In Java Category.

  
Question Answered By: Olga Kates   on Mar 28

Building quoted and escaped strings  in Java can be difficult when the
same sequences are the rule for the target (ie, the database engine).

Another db that uses this same pattern is Oracle, btw.

If you build a string  s in Java that contains escape sequences, you
must also escape (prefix with the \ character) each character that
might be recognized by the Java parser.

So if you want to send "XYZ" to the target as an escaped sequence
\"XYZ\" you'll have to add some additional escapes so Java won't act
on the characters  first:
\\\"XYZ\\\"
Where \\ escapes the escape character so Java won't act on it.
\" does as Anthony explained, only here so Java won't act on it.
The sequence is repeated at the end.

Then, you put it into a Java string:
s = "\\\"XYZ\\\"";

This is where the declaration of contants can help clarify your
intent:

final String ESC = "\\"; // escaped escape
final String DQ = "\""; // escaped double quote

s = ESC + DQ + "XYZ" + ESC + DQ;

Well, I think it clarifies! ;-) Anyway, now s is ready to be included
as the parameter to the SQL or whatever. The escapes and quotes  will
be sent along with the text.

Share: 

 

This Question has 1 more answer(s). View Complete Question Thread

 
Didn't find what you were looking for? Find more on Java Strings and restricted characters Or get search suggestion and latest updates.


Tagged: