问题
Any similar method to sprintf in Java?
回答1:
Complicated way (using Formatter)
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")
Or simpler way:
String.format
回答2:
In a way the String.format is like having a Java sprintf method available here
String status = String.format("The rename status is (%d)", RENAME_SUCCEEDED);
You can see the example here as well
回答3:
You're looking for
String.format.
回答4:
Yes: Formatted Printing for Java (sprintf)
回答5:
Have a look at the Formatter classs and Javadoc:
http://download.oracle.com/javase/6/docs/api/java/util/Formatter.html
回答6:
Apparently the irrational prejudice against unsigned numeric data extends to this as well. The %u format element is conspicuously absent from the supported set.
来源:https://stackoverflow.com/questions/5244457/any-method-similar-to-sprintf-in-java