This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Gayathri Chowduru
Ranch Hand
Posts: 42
  • 0
  • Mark post as helpful
  • send pies
  • 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?
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
Joanne Neal
Rancher
Pie
Posts: 3742
16
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic