I have summarized three placeholder forms here: just look at the following code
- String stringFormat = "lexical error at position %s, encountered %s, expected %s ";
- String messageFormat ="lexical error at position {0}, encountered {1}, expected {2}";
- System.out.println(String.format(stringFormat, 123, 100, 456));
- System.out.println(MessageFormat.format(messageFormat, new Date(), 100, 456));
The above are two common forms of use, here is another:
- %n$ms: represents the output is a string, n represents the number of parameters, setting the value of m can place a space before the output
- %n$md: represents the output is an integer, n represents the number of parameters, the value of m can be set to place a space before the output, or it can be set to 0m, and m 0s are placed before the output
- %n$mf: indicates that the output is a floating-point number, and n represents the number of parameters. Setting the value of m can control the number of decimal places. For example, when m= 2.2 , the output format is 00.00
- Example of use:
- String format = "%1$-25s%2$-48s";
- System.out.format(format, "111","222");