Reuse a parameter in String.format?

前端 未结 4 853
南笙
南笙 2020-12-07 11:14
String hello = \"Hello\";

String.format(\"%s %s %s %s %s %s\", hello, hello, hello, hello, hello, hello);

hello hello hello hello hello hello 

Do

4条回答
  •  鱼传尺愫
    2020-12-07 11:48

    From the docs:

    • 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.

    String.format("%1$s %1$s %1$s %1$s %1$s %1$s", hello);
    

提交回复
热议问题