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 theformat()
method using the class nameString
.str
is a string that is to be formatted...
in the above code signifies you can pass more than one object toformat()
.
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:
Specifier | Description |
---|---|
%b , %B | "true" or "false" based on the argument |
%s , %S | a string |
%c , %C | a Unicode character |
%d | a decimal integer (used for integers only) |
%o | an octal integer (used for integers only) |
%x , %X | a hexadecimal integer (used for integers only) |
%e , %E | for scientific notation (used for floating-point numbers) |
%f | for 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
的十六进制值。