String. format () format string, String format string
1. Several Common conversion Operators
Description of the conversion operator % d Integer type (decimal) 99% f floating point type 99.99% s string type "mingrisoft" % c character type 'M' % B Boolean Type true % Percent type % n linefeed
Package com. app; public class Test1 {public static void main (String [] args) {String str1 = String. format ("Hi, % s", "Wang Li"); System. out. println (str1); String str2 = String. format ("Hi, % s: % s. % s "," Wang Nan "," Wang Li "," Wang Zhang "); System. out. println (str2); System. out. printf ("half of 100 is: % d % n", 100/2); System. out. printf ("half of 100 is: % n % d", 100/2); // a line break will be generated when 50 is output }}
Result:
Hi, Wang Li
Hi, Wang Nan: Wang Li. Wang Zhang
Half of 100 is: 50
Half of 100 is:
50
2. Matching identifier
Indicates that the instance result + is a positive number or a negative number is added with the symbol ("% + d", 15) + 15 0 before the number 0 ("% 04d", 99) 0099 $ formatted parameter indexes ("% 1 $ d, % 2 $ s", 99, "abc") 99, abc
Package com. app; public class Test1 {public static void main (String [] args) {// $ use String str1 = String. format ("use of format parameter $: % 1 $ d, % 1 $ s", 99, "abc"); System. out. println (str1); String str2 = String. format ("use of format parameter $: % 1 $ d, % 2 $ s", 99, "abc"); System. out. println (str2) ;}} format parameter $ use: 99, 99 format parameter $ use: 99, abc