• 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

How to replace a single quote in a string with two single quotes

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

I have a string as
String comments = don't do canada.

I want to replace this single quote with two single quotes.

I tried like
char c;
for(int i=0; i<comments.length(); i++){
c = comments.charAt(i);
if(c == '\''){
comments.replace("\'","\'\'");
}
}
System.out.println("The replaced string is : " + comments);

This worked fine in java version 1.5

But it is throwing some error in java version 1.4.2.10

Can anybody pls tell me how to replace a single quote with two single quotes?
 
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


But it is throwing some error in java version 1.4.2.10


Its always helpful to include the error message when posting a question - it stops us having to guess what your issue might be.

Check the JavaDocs for String.replace(String, String). You'll notice that this method was added as of version 1.5.
 
Gayathri Chowduru
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Paul Sturrock ,

I got the output with String.replace(String,String) for jdk 1.5.

But here we are using jdk1.4.2.10

So, during compilation, it has shown an error as

replace(char,char) in java.lang.String cannot be applied to (java.lang.String,java.lang.String)

My intention is, I just need to check if there is character like single quote. If so, I want to replace that with two single quotes.

Plese let me know what would be the solution to get rid of this problem using jdk1.4.2.10
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You could probably implement something with the String.indexOf and String.substring methods.

Going back to your original code, String.replace replaces all instances of the searched for String, so you don't need to put it in a loop. You do however have to assign the value returned by the replace method to a variable.
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In JDK 1.4, you can't use that version of replace(). You can however use replaceAll(). Despite the very poorly chosen names of these methods, the difference between them is not that one replaces "all" and the other doesn't. The difference is that one uses regular expressions and the other doesn't. If you use replaceAll(), you may need to learn about regular expressions to understand some details of its behavior. However to replace a single ',this will probably not e an issue. Give it a try.
reply
    Bookmark Topic Watch Topic
  • New Topic