What does “%1$#” mean when used in String.format (Java)?

后端 未结 1 883
礼貌的吻别
礼貌的吻别 2021-02-02 12:46

Language is Java. What does the %1$# mean in...

static String padright (String str, int num) {
   return String.format(\"%1$#\" + num + \"str\", str         


        
1条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-02-02 13:23

    Template:

    %[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.

    The optional flags is a set of characters that modify the output format. The set of valid flags depends on the conversion.

    The optional width is a decimal integer indicating the minimum number of characters to be written to the output.

    The optional precision is a non-negative decimal integer usually used to restrict the number of characters. The specific behavior depends on the conversion.

    The required conversion is a character indicating how the argument should be formatted. The set of valid conversions for a given argument depends on the argument's data type.

    %1$ refers to the first substitution. In this case the string str. # is flag which says the result should use a conversion-dependent alternate form.

    http://download.oracle.com/javase/1.5.0/docs/api/java/util/Formatter.html

    0 讨论(0)
提交回复
热议问题