aspose file tools*
The moose likes Java in General and the fly likes How to replace a single quote in a string with two single quotes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "How to replace a single quote in a string with two single quotes " Watch "How to replace a single quote in a string with two single quotes " New topic
Author

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

Gayathri Chowduru
Ranch Hand

Joined: Oct 17, 2007
Posts: 42
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

Joined: Apr 14, 2004
Posts: 10336


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.


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Gayathri Chowduru
Ranch Hand

Joined: Oct 17, 2007
Posts: 42
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

Joined: Aug 05, 2005
Posts: 3742
    
  16
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.


Joanne
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
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'm not back." - Bill Harding, Twister
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: How to replace a single quote in a string with two single quotes