String hello = "Hello";
String.format("%s %s %s %s %s %s", hello, hello, hello, hello, hello, hello);
hello hello hello hello hello hello
hello
变量是否需要在调用 format 方法时重复多次,或者是否有一个速记版本可以让您指定一次参数以应用于所有 %s
个 token ?
最佳答案
来自 the docs :
- The format specifiers for general, character, and numeric types have the following syntax:
%[argument_index$][flags][width][.precision]conversion
可选的argument_index是一个十进制整数,表示参数在参数列表中的位置。第一个参数由 "1$"
引用,第二个参数由 "2$"
引用,以此类推
String.format("%1$s %1$s %1$s %1$s %1$s %1$s", hello);
关于java - 重用 String.format 中的参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6891175/