假设我想返回这样的东西: "Title 书名, 作者 作者名: $价格"
粗体部分是变量。 我无法使用string.format("%s .., blah blah")来返回这个结果。
String title = "bookTitle";
String author = "bookAuthor";
double cost = 100;
String text = String.format("Title %s, Author %s: $%.2f", title, author, cost);
System.out.println(text); // prints "Title bookTitle, Author bookAuthor: $100.00"
// or System.out.pritnf("Title %s, Author %s: $%.2f", title, author, cost);
两个字符串和一个精度为2的浮点数
String.format("Title %s, Author %s: $%f.2", bookTitle, bookAuthor, cost);
String fs;
fs = String.format("Title %t, Author %a: $%c", bookTitle, bookAuthor, cost);
System.out.println(fs);
http://download.oracle.com/javase/tutorial/java/data/strings.html