Java字符串格式方法大全
下文笔者讲述java字符串格式化的方法分享,如下所示
实现思路: 我们可以使用String.format对输入的字符串进行格式化
转换的通配符
占位符 | 类别 | 备注 |
%b , %B | 一般 | 真假 |
%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 | 约会时间 | 日期和时间转换的前缀 |
%% | 百分 | 显示文字'%' |
%n | 行分隔符 新行 | System.getProperty("line.separator") |
String.format示例分享
package com.java265; public class JavaStringFormat1 { public static void main(String[] args) { String result = String.format("%s is %d", "java265", 18); // java265 is 18 System.out.println(result); String result2 = String.format("%d + %d = %d", 1, 2, 1 + 2); // 1 + 2 = 3 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); } }
重新排列参数信息
使用%{index}${conversion} %1$ –第一个参数 %2$ –第二个参数 %3$ –第三个参数 %{index}$ – {index}参数
package com.java265; public class JavaStringFormat2 { public static void main(String[] args) { String a1 = "java1"; String a2 = "java2"; Integer a3 = 888; String result = String.format("Test: %3$d, %1$s, %1$s, %2$s", a1, a2, a3); System.out.println(result); } } ------运行以上代码,将输出以下信息----- Test: 888, java1, java1, java2
字符串占位符示例分享
package com.java265; 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; } } ----运行以上代码,将输出以下信息---- |Hello World| | Hello World| |Hello World | |Hello| |$$$$$$$$$Hello$World| $$$$$$$$$Hello World
String.format输出整数
package com.java265; 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|
String.format输出浮点数
package com.java265; 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); } } -----运行以上代码,将输出以下信息----- 3.141593 3.14 3.141593e+00 0x1.921fb54442d18p1 | 3.141593| |3.141593 |
String.format注意事项: 当左边需使用0进行补充时 可使用String.format实现,如下例所示 // 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");
版权声明
本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。