java placeholder

I have summarized three placeholder forms here: just look at the following code

[java]  view plain copy  
  1. String stringFormat  = "lexical error at position %s, encountered %s, expected %s ";   
  2.   
  3. String messageFormat ="lexical error at position {0}, encountered {1}, expected {2}";  
  4.   
  5. System.out.println(String.format(stringFormat, 123100456));  
  6.   
  7. System.out.println(MessageFormat.format(messageFormat, new Date(), 100456));  

The above are two common forms of use, here is another:

[java]  view plain copy  
  1. %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   
  2.   
  3.   
  4. %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   
  5.   
  6.   
  7. %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  

[html]  view plain copy  
  1. Example of use:  
  2.   
  3. String format = "%1$-25s%2$-48s";  
  4.   
  5. System.out.format(format, "111","222");  

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325945769&siteId=291194637