任何类似于 Java 中的 sprintf 的方法?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5244457/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Any method similar to sprintf in Java?
提问by user496949
Any similar method to sprintf in Java?
任何与 Java 中 sprintf 类似的方法?
采纳答案by Dewfy
Complicated way (using Formatter)
复杂的方式(使用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("%4s %3s %2s %1s", "a", "b", "c", "d")
Or simpler way:
或者更简单的方法:
String.format
回答by GustyWind
回答by Vladimir Ivanov
回答by Bart Kiers
回答by Puce
Have a look at the Formatter classs and Javadoc:
看看 Formatter 类和 Javadoc:
http://download.oracle.com/javase/6/docs/api/java/util/Formatter.html
http://download.oracle.com/javase/6/docs/api/java/util/Formatter.html
回答by Jerry Miller
Apparently the irrational prejudice against unsigned numeric data extends to this as well. The %u format element is conspicuously absent from the supported set.
显然,对无符号数字数据的非理性偏见也扩展到了这一点。%u 格式元素在支持的集合中明显不存在。