跳到主要内容

Java 字符串 format() 方法

The format() method returns a formatted string based on the argument passed.

Example

class Main {
public static void main(String[] args) {

String str = "Java";

// format string
String formatStr = String.format("Language: %s", str);

System.out.println(formatStr);
}
}

// Output: Language: Java

format() Syntax

The syntax of the String format() method is:

String.format(String str, Object... args)

Here,

  • format() is a static method. We call the format() method using the class name String.
  • str is a string that is to be formatted
  • ... in the above code signifies you can pass more than one object to format().

format() Parameters

The format() method takes two parameters.

  • format - a format string
  • args - 0 or more arguments

format() Return Value

  • returns a formatted string

Example 1: Java String format()

class Main {
public static void main(String[] args) {
String language = "Java";
int number = 30;
String result;

// format object as a string
result = String.format("Language: %s", language);

System.out.println(result); // Language: Java

// format number as a hexadecimal number
result = String.format("Hexadecimal Number: %x", number); // 1e

System.out.println(result); // Hexadecimal Number: 1e
}
}

In the above program, notice the code

result = String.format("Language: %s", language);

Here, "Language: %s" is a format string.

%s in the format string is replaced with the content of language. %s is a format specifier.

Similarly, %x is replaced with the hexadecimal value of number in String.format("Number: %x", number).

Format Specifiers

Here are the commonly used format specifiers:

SpecifierDescription
%b, %B"true" or "false" based on the argument
%s, %Sa string
%c, %Ca Unicode character
%da decimal integer (used for integers only)
%oan octal integer (used for integers only)
%x, %Xa hexadecimal integer (used for integers only)
%e, %Efor scientific notation (used for floating-point numbers)
%ffor decimal numbers (used for floating-point numbers)

Example 2: String Formatting of Numbers

class Main {
public static void main(String[] args) {
int n1 = 47;
float n2 = 35.864f;
double n3 = 44534345.76d;

// format as an octal number
System.out.println(String.format("n1 in octal: %o", n1)); // 57


// format as hexadecimal numbers
System.out.println(String.format("n1 in hexadecimal: %x", n1)); // 2f
System.out.println(String.format("n1 in hexadecimal: %X", n1)); // 2F

// format as strings
System.out.println(String.format("n1 as string: %s", n1)); // 47
System.out.println(String.format("n2 as string: %s", n2)); // 35.864

// format in scientific notation
System.out.println(String.format("n3 in scientific notation: %g", n3)); // 4.45343e+07
}
}

输出

n1 以八进制表示: 57
n1 以十六进制表示: 2f
n1 以十六进制表示: 2F
n1 作为字符串: 47
n2 作为字符串: 35.864
n3 以科学计数法表示: 4.45343e+07

示例 3:带有多个格式说明符的字符串格式化

你可以在格式字符串中使用多个格式说明符。

// 在格式字符串中使用多个格式说明符
class Main {
public static void main(String[] args) {
int n1 = 47;
String text = "结果";

System.out.println(String.format("%s\n十六进制: %x", text, n1));
}
}

输出

结果
十六进制: 2f

这里,%s 被替换为 text 的值。类似地,%o 被替换为 n1 的十六进制值。

在 Java 中字符串格式化时,格式说明符被替换为对象值

示例 4:小数的格式化

class Main {
public static void main(String[] args) {

float n1 = -452.534f;
double n2 = -345.766d;

// 原样格式化浮点数
System.out.println(String.format("n1 = %f", n1)); // -452.533997

System.out.println(String.format("n2 = %f", n2)); // -345.766000

// 显示两位小数
System.out.println(String.format("n1 = %.2f", n1)); // -452.53

System.out.println(String.format("n2 = %.2f", n2)); // -345.77
}
}

输出

n1 = -452.533997
n2 = -345.766000
n1 = -452.53
n2 = -345.77

注意: 当我们使用 %f 格式化 -452.534 时,得到的是 -452.533997。这不是 format() 方法的问题。Java 不会返回 浮点数的精确表示

当使用 %.2f 格式说明符时,format() 会在小数点后显示两位数字。

示例 5:用空格和 0 填充数字

// 在格式字符串中使用多个格式说明符
class Main {
public static void main(String[] args) {
int n1 = 46, n2 = -46;
String result;

// 用空格填充数字
// 字符串长度将为 5
result = String.format("|%5d|", n1); // | 46|

System.out.println(result);

// 用数字 0 填充数字
// 字符串长度将为 5
result = String.format("|%05d|", n1); // |00046|

System.out.println(result);

// 在数字前使用符号
result = String.format("%+d", n1); // +46

System.out.println(result);

result = String.format("%+d", n2); // -46

System.out.println(result);

// 用括号包围负数
// 并去掉符号
result = String.format("%(d", n2); // (46)

System.out.println(result);
}
}

示例 6:在十六进制和八进制前使用 0x 和 0

// 在十六进制前使用 0x
// 在八进制前使用 0
class Main {
public static void main(String[] args) {
int n = 46;

System.out.println(String.format("%#o", n)); // 056
System.out.println(String.format("%#x", n)); // 0x2e

}
}

Java String format() 方法的区域设置

如果你需要处理特定的区域设置,String 类的 format() 方法也有另一种语法。

String.format(Locale l,
String format,
Object... args)

示例 7:在 format() 中使用德国区域设置

// 使用 Locale
import java.util.Locale;

class Main {
public static void main(String[] args) {
int number = 8652145;
String result;

// 使用当前区域设置
result = String.format("数字: %,d", number);

System.out.println(result);

// 使用德国区域设置作为第一个参数
result = String.format(Locale.GERMAN, "德国数字: %,d", number);

System.out.println(result);
}
}

输出

数字: 8,652,145
德国数字: 8.652.145

注意: 在德国,整数的千位分隔符是 . 而不是 ,