File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes String replace function Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "String replace function" Watch "String replace function" New topic

String replace function

Mike Hogeboom

Joined: Oct 31, 2001
Posts: 19
Does anyone have a function that will replace stings within a string with new values.
e.g. public String(String theString, String replace, String replaceWith)
This function would replace all occurances of "replace" with "replaceWith" in the original "theString". it would then return this new string
Marilyn de Queiroz

Joined: Jul 22, 2000
Posts: 9059
Check out the Str class here which has lots of useful methods. Otherwise you could use the replace method in the StringBuffer class.

"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
Sadaf Zaidi

Joined: Oct 09, 2001
Posts: 29
replace method isuse to replace a character replace(char,char)
for example:
String cat="Sweet Sister";
String m = cat.replace('e','x');
Out put:
Swxxt Sistxr
it will replace the e with x
hope its clear
Julia Reynolds
Ranch Hand

Joined: May 31, 2001
Posts: 123
public static String replace(String s, String repl, String with)
if (repl == null)
return s;
if (with == null)
with = "";
int offset = 0;
int p = s.indexOf(repl);
while (p >= 0)
s = s.substring(0,p) + with + s.substring(p + repl.length());
offset = p + with.length();
p = s.indexOf(repl,offset);
return s;
I agree. Here's the link:
subject: String replace function
It's not a secret anymore!