Any method similar to sprintf in Java?

笑着哭i 提交于 2019-12-05 04:13:34

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

String.format.

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.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!