Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

multiple strings replacement

 
Sachin Deshpande
Greenhorn
Posts: 25
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 262
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here you go:


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

http://www.stringtemplate.org/
 
Sachin Deshpande
Greenhorn
Posts: 25
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thats a clean way to do it. and its pretty uselful to me. Thanx a zillion.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic