Table of Contents
ToggleJava String format Method
指定的格式字串和參數返回格式化的字串,可以使用此方法連接字串,格式化輸出連接的字串,如果格式為空,會拋出 NullPointerException ,如果指定的格式不合法或參數不足,會拋出 IllegalFormatException ,類似 c 語言的 sprintf 方法和 java 的 printf 方法, Format String Java 本篇增加了範例,並透過單元測試來驗證產出結果。
檔案目錄
./
+- src
+- test
| +- org
| +- ruoxue
| +- java_147
| +- string
| +- StringFormatTest.java
單元測試
Java Format String 提供格式化字串、整數、浮點數及填充等操作。
string
格式化字串,及長度為 10 向右對齊。
@Test
public void string() {
String value = "Java 147";
String formatted = String.format("String is: %s", value);
System.out.println(formatted);
formatted = String.format("String is: %10s", value);
System.out.println(formatted);
}
String is: Java 147
String is: Java 147
charz
格式化字元,及長度為 10 向右對齊。
@Test
public void charz() {
char value = 'J';
String formatted = String.format("Char is: %c", value);
System.out.println(formatted);
formatted = String.format("Char is: %10c", value);
System.out.println(formatted);
}
Char is: J
Char is: J
bolleanz
Java Format String 格式化布林,及長度為 10 向右對齊。
@Test
public void bolleanz() {
boolean value = true;
String formatted = String.format("Boolean is: %b", value);
System.out.println(formatted);
value = false;
formatted = String.format("Boolean is: %10b", value);
System.out.println(formatted);
}
Boolean is: true
Boolean is: false
integer
Java Format String 格式化整數支援多種資料類型,將其格式化為字串類型。
@Test
public void integer() {
byte byteValue = Byte.MAX_VALUE;
String formatted = String.format("Byte is: %d", byteValue);
System.out.println(formatted);
short shortValue = Short.MAX_VALUE;
formatted = String.format("Short is: %d", shortValue);
System.out.println(formatted);
int intValue = Integer.MAX_VALUE;
formatted = String.format("Integer is: %d", intValue);
System.out.println(formatted);
long longValue = Long.MAX_VALUE;
formatted = String.format("Long is: %d", longValue);
System.out.println(formatted);
BigInteger bigIntegerValue = BigInteger.ZERO;
formatted = String.format("BigInteger is: %d", bigIntegerValue);
System.out.println(formatted);
}
Byte is: 127
Short is: 32767
Integer is: 2147483647
Long is: 9223372036854775807
BigInteger is: 0
hex
格式化整數為 16 進位字串,及長度為 10 向右對齊。
@Test
public void hex() {
int intValue = Integer.MAX_VALUE;
String formatted = String.format("Hex is: %x", intValue);
System.out.println(formatted);
formatted = String.format("Hex is: %10x", intValue);
System.out.println(formatted);
formatted = String.format("Hex is: %010x", intValue);
System.out.println(formatted);
}
Hex is: 7fffffff
Hex is: 7fffffff
Hex is: 007fffffff
floating
格式化浮點數,及長度為 10 向右對齊。
@Test
public void floating() {
float floatValue = Float.MAX_VALUE;
String formatted = String.format("Float is: %f", floatValue);
System.out.println(formatted);
floatValue = 1.47f;
formatted = String.format("Float is: %10.4f", floatValue);
System.out.println(formatted);
double doubleValue = Double.MAX_VALUE;
formatted = String.format("Double is: %e", doubleValue);
System.out.println(formatted);
}
Float is: 340282346638528860000000000000000000000.000000
Float is: 1.4700
Double is: 1.797693e+308
padding
格式化整數,及長度為 10 向右或向左對齊,填充 0 。
@Test
public void padding() {
int intValue = 147;
String formatted = String.format("%010d", intValue);
System.out.println(formatted);
formatted = String.format("% d", intValue);
System.out.println(formatted);
formatted = String.format("%10d", intValue);
System.out.println(formatted);
formatted = String.format("%-10d", intValue);
System.out.println(formatted);
}
0000000147
147
147
147
StringFormatTest.java
String Format in Java 新增單元測試,驗證是否符合預期。
package org.ruoxue.java_147.string;
import java.math.BigInteger;
import org.junit.Test;
public class StringFormatTest {
@Test
public void string() {
String value = "Java 147";
String formatted = String.format("String is: %s", value);
System.out.println(formatted);
formatted = String.format("String is: %10s", value);
System.out.println(formatted);
}
@Test
public void charz() {
char value = 'J';
String formatted = String.format("Char is: %c", value);
System.out.println(formatted);
formatted = String.format("Char is: %10c", value);
System.out.println(formatted);
}
@Test
public void bolleanz() {
boolean value = true;
String formatted = String.format("Boolean is: %b", value);
System.out.println(formatted);
value = false;
formatted = String.format("Boolean is: %10b", value);
System.out.println(formatted);
}
@Test
public void integer() {
byte byteValue = Byte.MAX_VALUE;
String formatted = String.format("Byte is: %d", byteValue);
System.out.println(formatted);
short shortValue = Short.MAX_VALUE;
formatted = String.format("Short is: %d", shortValue);
System.out.println(formatted);
int intValue = Integer.MAX_VALUE;
formatted = String.format("Integer is: %d", intValue);
System.out.println(formatted);
long longValue = Long.MAX_VALUE;
formatted = String.format("Long is: %d", longValue);
System.out.println(formatted);
BigInteger bigIntegerValue = BigInteger.ZERO;
formatted = String.format("BigInteger is: %d", bigIntegerValue);
System.out.println(formatted);
}
@Test
public void hex() {
int intValue = Integer.MAX_VALUE;
String formatted = String.format("Hex is: %x", intValue);
System.out.println(formatted);
formatted = String.format("Hex is: %10x", intValue);
System.out.println(formatted);
formatted = String.format("Hex is: %010x", intValue);
System.out.println(formatted);
}
@Test
public void floating() {
float floatValue = Float.MAX_VALUE;
String formatted = String.format("Float is: %f", floatValue);
System.out.println(formatted);
floatValue = 1.47f;
formatted = String.format("Float is: %10.4f", floatValue);
System.out.println(formatted);
double doubleValue = Double.MAX_VALUE;
formatted = String.format("Double is: %e", doubleValue);
System.out.println(formatted);
}
@Test
public void padding() {
int intValue = 147;
String formatted = String.format("%010d", intValue);
System.out.println(formatted);
formatted = String.format("% d", intValue);
System.out.println(formatted);
formatted = String.format("%10d", intValue);
System.out.println(formatted);
formatted = String.format("%-10d", intValue);
System.out.println(formatted);
}
}
心得分享
String Format Java 可以設置任意值的寬度、填充等,提供了幾種 Format 常見方法的操作範例, String Format in Java 支援各種不同類型的格式化,方便簡單的設定,快速產生需要的格式化字串。