Any similar method to sprintf in Java?
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
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
You're looking for
Have a look at the Formatter classs and Javadoc:
http://download.oracle.com/javase/6/docs/api/java/util/Formatter.html
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