本文向你展示如何通过 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
package com.mkyong;
public class JavaStringFormat1 {
public static void main(String[] args) {
String result = String.format("%s is %d", "mkyong", 38); // mkyong is 38
System.out.println(result);
String result2 = String.format("%d + %d = %d", 1, 1, 1 + 1); // 1 + 1 = 2
System.out.println(result2);
String result3 = String.format("%s = %f", "PI", Math.PI); // PI = 3.141593
String result4 = String.format("%s = %.3f", "PI", Math.PI); // PI = 3.142
System.out.println(result3);
System.out.println(result4);
}
}
Output
mkyong is 38
1 + 1 = 2
PI = 3.141593
PI = 3.142
2. Argument
我们可以使用来重新排列参数%{index}${conversion}
。
%1$
–第一个论点%2$
–第二个论点%3$
–第三个论点%{index}$
– {index}参数
JavaStringFormat2.java
package com.mkyong;
public class JavaStringFormat2 {
public static void main(String[] args) {
String a1 = "hello1";
String a2 = "hello2";
Integer a3 = 333;
String result = String.format("Test: %3$d, %1$s, %1$s, %2$s", a1, a2, a3);
System.out.println(result);
}
}
Output
Test: 333, hello1, hello1, hello2
3. String
JavaStringFormat3.java
package com.mkyong;
public class JavaStringFormat3 {
public static void main(String[] args) {
String input = "Hello World";
// default
String result1 = String.format("|%s|", input); // |Hello World|
// this %s has length of 20, format as %20s, pad 20 characters
// default right-justified
String result2 = String.format("|%20s|", "Hello World"); // | Hello World|
// left-justified
String result3 = String.format("|%-20s|", "Hello World"); // |Hello World |
// max length = 5
String result4 = String.format("|%.5s|", "Hello World"); // |Hello|
// left pad with $
String result5 = String.format("|%20s|", "Hello World") // |$$$$$$$$$Hello$World|
.replace(' ', '$');
// left pad with $, ignore spaces in string
String result6 = padLeft("Hello World", 20, "$"); // $$$$$$$$$Hello World
System.out.println(result1);
System.out.println(result2);
System.out.println(result3);
System.out.println(result4);
System.out.println(result5);
System.out.println(result6);
}
public static String padLeft(String str, int width, String padWith) {
String result = "";
String temp = String.format("%" + width + "s", str);
if (temp.length() > str.length()) {
result = temp.substring(0, temp.length() - str.length()).replace(" ", padWith);
}
result += str;
return result;
}
}
Output
|Hello World|
| Hello World|
|Hello World |
|Hello|
|$$$$$$$$$Hello$World|
$$$$$$$$$Hello World
4. Integer
JavaStringFormat4.java
package com.mkyong;
public class JavaStringFormat4 {
public static void main(String[] args) {
// default
String result1 = String.format("|%d|", 100); // |100|
// this %d has length of 20, format as %20d, pad 20 characters
// default right-justified
String result2 = String.format("|%20d|", 100); // | 100|
// left-justified
String result3 = String.format("|%-20d|", 100); // |100 |
// left pad with 0
String result4 = String.format("|%020d|", 100); // |00000000000000000100|
// prefix with +
String result5 = String.format("|%+20d|", 100); // | +100|
// negative
String result6 = String.format("|%20d|", -100); // | -100|
// octal
String result7 = String.format("|%20o|", 100); // | 144|
// hex
String result8 = String.format("|%20x|", 30); // | 1e|
// prefix 0x with #
String result9 = String.format("|%#20x|", 30); // | 0x1e|
System.out.println(result1);
System.out.println(result2);
System.out.println(result3);
System.out.println(result4);
System.out.println(result5);
System.out.println(result6);
System.out.println(result7);
System.out.println(result8);
System.out.println(result9);
}
}
输出量
|100|
| 100|
|100 |
|00000000000000000100|
| +100|
| -100|
| 144|
| 1e|
| 0x1e|
5. Floating Points(浮点数)
JavaStringFormat5.java
package com.mkyong;
public class JavaStringFormat5 {
public static void main(String[] args) {
double pi = Math.PI;
// default
String result1 = String.format("%f", pi); // 3.141593
// 2 decimal points
String result2 = String.format("%.2f", pi); // 3.14
String result3 = String.format("%e", pi); // 3.141593e+00
String result4 = String.format("%a", pi); // 0x1.921fb54442d18p1
// right
String result5 = String.format("|%20f|", pi); // | 3.141593|
// left
String result6 = String.format("|%-20f|", pi); // |3.141593 |
System.out.println(result1);
System.out.println(result2);
System.out.println(result3);
System.out.println(result4);
System.out.println(result5);
System.out.println(result6);
}
}
Output
3.141593
3.14
3.141593e+00
0x1.921fb54442d18p1
| 3.141593|
|3.141593 |
6.常见问题
6.1 将十进制转换为二进制(4个字节),并用0填充。
// 1100100
String result1 = String.format("%s", Integer.toBinaryString(100));
// 00000000000000000000000001100100
String result2 = String.format("%32s", Integer.toBinaryString(100)).replace(" ", "0");
// 00000000000000011110001001000000
String result3 = String.format("%32s", Integer.toBinaryString(123456)).replace(" ", "0");