• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Special characters

 
Ranch Hand
Posts: 398
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a string that is passed at runtime. I need to parse it and massage the string to be in aceptable form to the database. If I have an apostrophe, I need to ad another in front of it. For example "King's royal duties" should be sent as "King''s royal duties".

I have something like this..

[code]

treatSpeicalChars(String s){

StringBuffer x = new StringBuffer();

for(int i =0;i<s.length;i++){

char ch = charAt(i);

switch(ch){

case '\'' :

x.append("''");
break;

//similarly for other speical chars..
// do something differnt...
default :
x.append(ch);

}

return x.toString();


But my string is returned as is, without the extra "'" added. I tried parsing with extra '\' s for all chars as well. Ideas?



Thanks,

Vasu
 
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why don't you post the core you really tried? This one won't
even compile:

and missing } at the end.

P.
 
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by vasu maj:
I have a string that is passed at runtime. I need to parse it and massage the string to be in aceptable form to the database.

Save yourself many a headache by using JDBC PreparedStatements. When you call ps.setString(str), the driver's implementation will take care of escaping special characters automatically.

You declare your statement with ?s to denote parameters to be substituted by you. Next you set the parameters and then execute the statement. They work similarly to regular Statements. It's been a while snice I've needed to do this, but from memory:
[ December 21, 2004: Message edited by: David Harkness ]
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic