什么是允许您写入字符串的Java功能?

时间:2011-07-27 15:20:20

标签: java string

我之前在Java中已经完成了这个,但我不记得究竟是怎么回事。

您创建一个字符串:

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

然后你可以在字符串中写上“卖”字。

你用它做什么?

5 个答案:

答案 0 :(得分:10)

String myString = "sells";
String foo = String.format("She %s sea shells by the seashore.", myString);

答案 1 :(得分:5)

您正在寻找Formatter classString.format convenience method

String.format(foo, "sells");

答案 2 :(得分: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)

答案 3 :(得分:2)

您可以使用String.Format功能。

答案 4 :(得分:1)

直接来源(和我的书签)......

   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");

更新:对不起意味着包含以下链接:http://download.oracle.com/javase/1.5.0/docs/api/java/util/Formatter.html

相关问题