Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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

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

In the Java API, String.format() is used in this way:

public static String format(String format, Object... args) 

So I think %1$# is a format specifier.

%[flags][width][.precision][argsize]typechar is the template.

  • 1 is a flag?
  • $ is the width?
  • # is the precision?
  • num is the argsize?
  • "str" is the typechar?

Is that right?

like image 322
theineffablebob Avatar asked Apr 23 '11 07:04

theineffablebob


People also ask

What does 1 mean in a text?

Definition: Active or Top Partner (typically used in gay dating) Type: Cyber Term.

What does it mean to be #1?

1 : first in rank, importance, or influence : foremost cancer is the country's number one killer —often written No. 1. 2 : of highest or of high quality.

What does #1 mean on Snapchat?

Summary of Key Points "Number one" is the most common definition for #1 on Snapchat, WhatsApp, Facebook, Twitter, Instagram, and TikTok.

What is the 1 in math?

In mathematics, the number 1 represents a single entity, a quantity or value of 1. The whole number between 0 and 2 is 1. The number name of 1 is one. Little Riri is showing 1 finger.


1 Answers

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

like image 121
manojlds Avatar answered Oct 02 '22 16:10

manojlds