October 16th, 2003, 11:03 AM
I'm Internet Famous
Join Date: Jan 2003
Location: Toronto, Canada
Time spent in forums: 1 Week 16 h 19 m 35 sec
Reputation Power: 18
I was playing around with the replace() method of the String class, only to be frustrated by the fact that it replaces characters, and not strings... i couldn't do String a = a.replace('a',"ab");
Before attempting to write my own method, i decided to search for one already written... i'm pleased to announce, i found one! =) [ya, i'm lazy]
This code was posted on another forum:
/* Replace all instances of a String in a String.
* @param s String to alter.
* @param f String to look for.
* @param r String to replace it with, or null to just remove it.
public String replace( String s, String f, String r )
if (s == null) return s;
if (f == null) return s;
if (r == null) r = "";
int index01 = s.indexOf( f );
while (index01 != -1)
s = s.substring(0,index01) + r + s.substring(index01+f.length());
index01 += r.length();
index01 = s.indexOf( f, index01 );
basically use the method like this:
replace(original_string, search_string, replace_with);
thus, for my purposes, i will do:
replace("I'm sure this works","\'","\\\'");
credit is given to "idarke" from the tek-tips.com forums