任何类似于 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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 10:11:39  来源:igfitidea点击:

Any method similar to sprintf in Java?

javaformatter

提问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

In a way the String.format is like having a Java sprintf method available here

在某种程度上,String.format 就像这里有一个可用的 Java sprintf 方法

String status = String.format("The rename status is (%d)", RENAME_SUCCEEDED);

You can see the examplehere as well

您也可以在此处查看示例

回答by Vladimir Ivanov

You're looking for

您正在寻找

String.format.

String.format.

回答by Puce

回答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 格式元素在支持的集合中明显不存在。