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

multiple strings replacement

Sachin Deshpande

Joined: Jan 31, 2001
Posts: 25
I need replace multiple (sub)Strings/ place holders in a (large)String with values in a single call. How to go about it? What is the best way? Is there anything available in java? I can use jdk1.4.2 only.

eg. String origStr = " 0={0} 1={1} 2={2} ";
String replacementVals[] = {"zero", "one", "two"};
String newStr = replace(origStr, replacementVals);
After running the code, newStr should contain " 0=zero 1=one 2=two ".

I have tried using java.text.MessageFormat but it accepts a max of 10 place holders only where my requirement is for more than 10 place holders. Any way in which to overcome this limitation? do you think reg-ex can help?
thanks in advance.
Alan Moore
Ranch Hand

Joined: May 06, 2004
Posts: 262
Here you go:

But if the task is that complex, you might consider using StringTemplate:
Sachin Deshpande

Joined: Jan 31, 2001
Posts: 25
Thats a clean way to do it. and its pretty uselful to me. Thanx a zillion.
I agree. Here's the link:
subject: multiple strings replacement
It's not a secret anymore!