Any method similar to sprintf in Java?

℡╲_俬逩灬. 提交于 2020-01-02 02:22:08

问题


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

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