java - 如何在 Scala 中以字符串格式重复参数

标签 java scala string-formatting

如何重复使用相同的字符串进行格式放置?例如

"%s-%s-%s" format("OK")
>> "OK-OK-OK"

最佳答案

这应该有效:

"%1$s-%1$s-%1$s" format "OK"

WrappedString格式方法在后台使用 java.util.Formatter。还有 Formatter Javadoc说:

The format specifiers for general, character, and numeric types have the following syntax:

%[argument_index$][flags][width][.precision]conversion

The optional argument_index is a decimal integer indicating the position of the argument in the argument list. The first argument is referenced by "1$", the second by "2$", etc.

关于java - 如何在 Scala 中以字符串格式重复参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13157584/

相关文章:

java - Scala 无法访问 Java 内部类?

python - 如何在 Python 中格式化具有可变位数的数字?

delphi - 如何格式化 MinExtended80Denormal?

c - 如何在 C 中将 float 格式化为右侧?

java - 在java中为各种用户定义的类创建通用方法

java - 返回值的Java并发方法调用

java - 如何知道在使用 Intellij 进入 catch block 之前执行的最后一行是什么?

java - Intellij Idea CE 2018.2.1 中资源目录不包含在输出目录中

java - 如何使用Spring Security仅保护一个URL并允许所有URL

scala - 无限可 curry 添加功能