Java String format()

format()方法根据传入的参数返回一个格式化后的字符串

示例

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() 语法

String format() 方法的语法是

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

这里,

  • format() 是一个静态方法。我们使用类名 String 来调用 format() 方法。
  • str 是要格式化的字符串
  • 上面的代码中的 ... 表示您可以向 format() 传递多个对象。

format() 参数

format() 方法接受两个参数。

  • format - 一个格式字符串
  • args - 0 个或多个参数

format() 返回值

  • 返回一个格式化后的字符串

示例 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 } }

在上面的程序中,请注意代码

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

这里,"Language: %s" 是一个格式字符串

格式字符串中的 %slanguage 的内容替换。%s 是一个格式说明符。

同样,在 String.format("Number: %x", number) 中,%xnumber 的十六进制值替换。


格式说明符

以下是一些常用的格式说明符

说明符 描述
%b, %B 根据参数返回 "true""false"
%s, %S 一个字符串
%c, %C 一个 Unicode 字符
%d 一个十进制整数(仅用于整数)
%o 一个八进制整数(仅用于整数)
%x, %X 一个十六进制整数(仅用于整数)
%e, %E 用于科学计数法(用于浮点数)
%f 用于十进制数(用于浮点数)

示例 2: 数字的字符串格式化

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 in octal: 57
n1 in hexadecimal: 2f
n1 in hexadecimal: 2F
n1 as string: 47
n2 as string: 35.864
n3 in scientific notation: 4.45343e+07

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

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

// using more than one format specifiers
// in a format string
class Main {
  public static void main(String[] args) {
    int n1 = 47;
    String text = "Result";

System.out.println(String.format("%s\nhexadecimal: %x", text, n1));
} }

输出

Result
hexadecimal: 2f

这里,%stext 的值替换。同样,%on1 的十六进制值替换。

Format specifier is replaced with the object value during string formatting in Java
Java String format() 的工作原理

示例 4: 十进制数的格式化

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

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

    // format floating-point as it is
System.out.println(String.format("n1 = %f", n1)); // -452.533997
System.out.println(String.format("n2 = %f", n2)); // -345.766000 // show up to two decimal places
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 填充数字

// using more than one format specifiers
// in a format string
class Main {
  public static void main(String[] args) {
    int n1 = 46, n2 = -46;
    String result;

    // padding number with spaces
    // the length of the string will be 5
result = String.format("|%5d|", n1); // | 46|
System.out.println(result); // padding number with numbers 0 // the length of the string will be 5
result = String.format("|%05d|", n1); // |00046|
System.out.println(result); // using signs before numbers
result = String.format("%+d", n1); // +46
System.out.println(result);
result = String.format("%+d", n2); // -46
System.out.println(result); // enclose negative number within parenthesis // and removing the sign
result = String.format("%(d", n2); // (46)
System.out.println(result); } }

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

// using 0x before hexadecimal
// using 0 before octal
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() 与 Locale

如果您需要使用指定的区域设置,String format() 方法还有另一种语法。

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

示例 7: 在 format() 中使用 GERMAN Locale

// to use Locale
import java.util.Locale;

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

    // using the current locale
result = String.format("Number: %,d", number);
System.out.println(result); // using the GERMAN locale as the first argument
result = String.format(Locale.GERMAN, "Number in German: %,d", number);
System.out.println(result); } }

输出

Number: 8,652,145
Number in German: 8.652.145

注意: 在德国,整数通常用 . 而不是 , 分隔。

你觉得这篇文章有帮助吗?

我们的高级学习平台,凭借十多年的经验和数千条反馈创建。

以前所未有的方式学习和提高您的编程技能。

试用 Programiz PRO
  • 交互式课程
  • 证书
  • AI 帮助
  • 2000+ 挑战