简体   繁体   中英

Any method similar to sprintf in Java?

在Java中使用sprintf的任何类似方法?

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 .

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM