views:

100

answers:

3

It would work something like this

someUtility.replace ("Hello, my name is {1}.  What is your {2}?", "Mark", "name");

Thanks in advance!

+3  A: 
String.format("Hello, my name is %1$s.  What is your %2$s?", "Mark", "name");

or if you will just use each once in order:

String.format("Hello, my name is %s.  What is your %s?", "Mark", "name");
Matthew Flaschen
as far as I know,printf does that too!
Phobia
I do it like this: String.format("Hello, my name is %s. What is your %s?", "Mark", "name");
Mondain
A: 

If you want to use Spring the Resource Bundles will give you the actual exact style of replacement that you have displayed. You can see some examples here: http://www.mkyong.com/spring/spring-resource-bundle-with-resourcebundlemessagesource-example/

Mondain
Android doesn't use Resource Bundles. The API for doing this with Android's resource system is: http://developer.android.com/reference/android/content/res/Resources.html#getString(int, java.lang.Object...)
hackbod
+2  A: 

Look at MessageFormat:

MessageFormat.format("Hello, my name is {0}.  What is your {1}?", "Mark", "name");

Usually I prefer String.format or System.out.printf though for c-style formatting (sort of), as Matthew suggested.

deepc
Almost, it's 0-indexed.
Matthew Flaschen