本文向你展示如何通过 String.format() 来格式化Java中的字符串。

这是总结。

Conversion 类别 描述
%b%B 一般 true or false
%h%H 一般 对象的哈希码值
%s%S 一般 字符串
%c%C 字符 Unicode字符
%d 整数 十进制整数
%o 整数 八进制整数,以8为底
%x%X 整数 十六进制整数,以16为底
%e%E 浮点 科学计数形式的十进制数字1.000000e + 02
%f 浮点 十进制数
%g%G 浮点 十进制数,四舍五入为精度
%a%A 浮点 十六进制浮点数,0x1.4333333333333p3
%t%T date/time 日期和时间转换的前缀
%% 百分比 display literal ‘%’
%n 换行符 new line, System.getProperty("line.separator")

进一步阅读-Formatter JavaDoc

1. Simple

JavaStringFormat1.java

  1. package com.mkyong;
  2. public class JavaStringFormat1 {
  3. public static void main(String[] args) {
  4. String result = String.format("%s is %d", "mkyong", 38); // mkyong is 38
  5. System.out.println(result);
  6. String result2 = String.format("%d + %d = %d", 1, 1, 1 + 1); // 1 + 1 = 2
  7. System.out.println(result2);
  8. String result3 = String.format("%s = %f", "PI", Math.PI); // PI = 3.141593
  9. String result4 = String.format("%s = %.3f", "PI", Math.PI); // PI = 3.142
  10. System.out.println(result3);
  11. System.out.println(result4);
  12. }
  13. }

Output

  1. mkyong is 38
  2. 1 + 1 = 2
  3. PI = 3.141593
  4. PI = 3.142

2. Argument

我们可以使用来重新排列参数%{index}${conversion}

  • %1$ –第一个论点
  • %2$ –第二个论点
  • %3$ –第三个论点
  • %{index}$ – {index}参数

JavaStringFormat2.java

  1. package com.mkyong;
  2. public class JavaStringFormat2 {
  3. public static void main(String[] args) {
  4. String a1 = "hello1";
  5. String a2 = "hello2";
  6. Integer a3 = 333;
  7. String result = String.format("Test: %3$d, %1$s, %1$s, %2$s", a1, a2, a3);
  8. System.out.println(result);
  9. }
  10. }

Output

  1. Test: 333, hello1, hello1, hello2

3. String

JavaStringFormat3.java

  1. package com.mkyong;
  2. public class JavaStringFormat3 {
  3. public static void main(String[] args) {
  4. String input = "Hello World";
  5. // default
  6. String result1 = String.format("|%s|", input); // |Hello World|
  7. // this %s has length of 20, format as %20s, pad 20 characters
  8. // default right-justified
  9. String result2 = String.format("|%20s|", "Hello World"); // | Hello World|
  10. // left-justified
  11. String result3 = String.format("|%-20s|", "Hello World"); // |Hello World |
  12. // max length = 5
  13. String result4 = String.format("|%.5s|", "Hello World"); // |Hello|
  14. // left pad with $
  15. String result5 = String.format("|%20s|", "Hello World") // |$$$$$$$$$Hello$World|
  16. .replace(' ', '$');
  17. // left pad with $, ignore spaces in string
  18. String result6 = padLeft("Hello World", 20, "$"); // $$$$$$$$$Hello World
  19. System.out.println(result1);
  20. System.out.println(result2);
  21. System.out.println(result3);
  22. System.out.println(result4);
  23. System.out.println(result5);
  24. System.out.println(result6);
  25. }
  26. public static String padLeft(String str, int width, String padWith) {
  27. String result = "";
  28. String temp = String.format("%" + width + "s", str);
  29. if (temp.length() > str.length()) {
  30. result = temp.substring(0, temp.length() - str.length()).replace(" ", padWith);
  31. }
  32. result += str;
  33. return result;
  34. }
  35. }

Output

  1. |Hello World|
  2. | Hello World|
  3. |Hello World |
  4. |Hello|
  5. |$$$$$$$$$Hello$World|
  6. $$$$$$$$$Hello World

4. Integer

JavaStringFormat4.java

  1. package com.mkyong;
  2. public class JavaStringFormat4 {
  3. public static void main(String[] args) {
  4. // default
  5. String result1 = String.format("|%d|", 100); // |100|
  6. // this %d has length of 20, format as %20d, pad 20 characters
  7. // default right-justified
  8. String result2 = String.format("|%20d|", 100); // | 100|
  9. // left-justified
  10. String result3 = String.format("|%-20d|", 100); // |100 |
  11. // left pad with 0
  12. String result4 = String.format("|%020d|", 100); // |00000000000000000100|
  13. // prefix with +
  14. String result5 = String.format("|%+20d|", 100); // | +100|
  15. // negative
  16. String result6 = String.format("|%20d|", -100); // | -100|
  17. // octal
  18. String result7 = String.format("|%20o|", 100); // | 144|
  19. // hex
  20. String result8 = String.format("|%20x|", 30); // | 1e|
  21. // prefix 0x with #
  22. String result9 = String.format("|%#20x|", 30); // | 0x1e|
  23. System.out.println(result1);
  24. System.out.println(result2);
  25. System.out.println(result3);
  26. System.out.println(result4);
  27. System.out.println(result5);
  28. System.out.println(result6);
  29. System.out.println(result7);
  30. System.out.println(result8);
  31. System.out.println(result9);
  32. }
  33. }

输出量

  1. |100|
  2. | 100|
  3. |100 |
  4. |00000000000000000100|
  5. | +100|
  6. | -100|
  7. | 144|
  8. | 1e|
  9. | 0x1e|

5. Floating Points(浮点数)

JavaStringFormat5.java

  1. package com.mkyong;
  2. public class JavaStringFormat5 {
  3. public static void main(String[] args) {
  4. double pi = Math.PI;
  5. // default
  6. String result1 = String.format("%f", pi); // 3.141593
  7. // 2 decimal points
  8. String result2 = String.format("%.2f", pi); // 3.14
  9. String result3 = String.format("%e", pi); // 3.141593e+00
  10. String result4 = String.format("%a", pi); // 0x1.921fb54442d18p1
  11. // right
  12. String result5 = String.format("|%20f|", pi); // | 3.141593|
  13. // left
  14. String result6 = String.format("|%-20f|", pi); // |3.141593 |
  15. System.out.println(result1);
  16. System.out.println(result2);
  17. System.out.println(result3);
  18. System.out.println(result4);
  19. System.out.println(result5);
  20. System.out.println(result6);
  21. }
  22. }

Output

  1. 3.141593
  2. 3.14
  3. 3.141593e+00
  4. 0x1.921fb54442d18p1
  5. | 3.141593|
  6. |3.141593 |

6.常见问题

6.1 将十进制转换为二进制(4个字节),并用0填充。

  1. // 1100100
  2. String result1 = String.format("%s", Integer.toBinaryString(100));
  3. // 00000000000000000000000001100100
  4. String result2 = String.format("%32s", Integer.toBinaryString(100)).replace(" ", "0");
  5. // 00000000000000011110001001000000
  6. String result3 = String.format("%32s", Integer.toBinaryString(123456)).replace(" ", "0");

参考文献

扩展阅读

JAVA字符串格式化-String.format()的使用