本文介绍了重用String.format中的参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
String hello = "Hello";
String.format("%s %s %s %s %s %s", hello, hello, hello, hello, hello, hello);
hello hello hello hello hello hello
在对format方法的调用中是否需要多次重复hello变量,还是有一个速记版本可以让您一次指定参数以应用于所有%s
标记?
Does the hello variable need to be repeated multiple times in the call to the format method or is there a shorthand version that lets you specify the argument once to be applied to all of the %s
tokens?
推荐答案
来自:
%[argument_index$][flags][width][.precision]conversion
可选的 argument_index 是一个十进制整数,指示参数在参数列表中的位置. "1$"
引用第一个参数,"2$"
引用第二个参数,等等.
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.
String.format("%1$s %1$s %1$s %1$s %1$s %1$s", hello);
这篇关于重用String.format中的参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!