This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Removing ' from string. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Removing Watch "Removing New topic

Removing ' from string.

Seshagiri Kamalakaran

Joined: Feb 12, 2004
Posts: 4
The strings contain more than one '(single quote). I want to replace it with \' (a back slash as escape sequence to render in javascript). The problem is the string can contain more than one '.
For example :
Paul's pet's name is jony.
The output shd be :
Paul\'s pet\'s name is jony.
Can any one help me in getting the logic to make this work?
Marc Peabody
pie sneak

Joined: Feb 05, 2003
Posts: 4727

The fact that the String can contain more than one ' doesn't make much for a problem. What isn't working?

A good workman is known by his tools.
Adrian Yan
Ranch Hand

Joined: Oct 02, 2000
Posts: 688
Use the replaceAll method of a String object:
Seshagiri Kamalakaran

Joined: Feb 12, 2004
Posts: 4
Thanks Yan for the solution... The thing is replaceAll method comes with java1.4 and at present we are using only java 1.3.. So using some string manipulations I have to do that...
Any other solution..?
Seshagiri Kamalakaran

Joined: Feb 12, 2004
Posts: 4
Hai I have found a solution .. but dont know whether this is efficient one..
public class StringExam
public static void main(String args[])
String original = "Paul's Pet's Name's is John's";
StringBuffer sb = new StringBuffer(original);
int i =0;
int j=0;
int cnt =0;
boolean flag = true;

char ch = original.charAt(i);

while(i < original.length());

System.out.println("Original string: " + original);
System.out.println("After replaceAll string: " + sb.toString());
I agree. Here's the link:
subject: Removing ' from string.
Similar Threads
Searching for a list element without having to iterate
basic high level question
Doubut reagarding application.xml
Good implementation for the hashCode() for String objects
PreparedStatement Vs Statement