2

I've done this in Java before but I can't remember how exactly.

You create a String:

String foo = "She %s sea shells by the seashore.";

Then you can write into the String the word "sells".

What do you use for that?

0

5 Answers 5

10
String myString = "sells";
String foo = String.format("She %s sea shells by the seashore.", myString);
Sign up to request clarification or add additional context in comments.

Comments

5

You're looking for the Formatter class, or the String.format convenience method:

String.format(foo, "sells");

Comments

3

String.format?

Object a[] = { "Real's HowTo", "http://www.any.com" ,
        java.util.Calendar.getInstance()};

String s = String.format("Welcome %1$s at %2$s ( %3$tY %3$tm %3$te )", a);
System.out.println(s);
//  output : Welcome Real's HowTo at http://www.any.com (2010 06 26)

Comments

2

You can use the String.Format function for this.

Comments

1

Straight from the source (and my bookmarks)...

   StringBuilder sb = new StringBuilder();
   // Send all output to the Appendable object sb
   Formatter formatter = new Formatter(sb, Locale.US);

   // Explicit argument indices may be used to re-order output.
   formatter.format("%4$2s %3$2s %2$2s %1$2s", "a", "b", "c", "d");

UPDATE: Sorry meant to include the link: http://download.oracle.com/javase/1.5.0/docs/api/java/util/Formatter.html

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.