📜  Java String format() 方法及示例

📅  最后修改于: 2022-05-13 01:55:00.780000             🧑  作者: Mango

Java String format() 方法及示例

在Java中, String format() 方法使用给定的语言环境、指定的格式字符串参数返回一个格式化的字符串。我们可以使用这种方法连接字符串,同时我们可以格式化输出的连接字符串。

语法:有两种类型的字符串format()方法

public static String format(Locale loc, String form, Object... args)
public static String format(String form, Object... args)

范围:

  • 要应用于 format() 方法的语言环境值
  • 输出字符串的格式。
  • 参数 指定格式字符串的参数数量。它可能为零或更多。

返回类型:格式化字符串。

抛出异常:

  • 空指针异常: 如果格式为空。
  • 非法格式异常: 如果指定的格式不合法或参数不足。

示例 1:

java
// Java program to demonstrate
// working of format() method
  
// Main class
class GFG {
  
    // MAin driver method
    public static void main(String args[])
    {
        // Custom input string to be formatted
        String str = "GeeksforGeeks";
  
        // Concatenation of two strings
        String s = String.format("My Company name is %s", str);
  
        // Output is given upto 8 decimal places
        String str2 = String.format("My answer is %.8f", 47.65734);
  
        // Here answer is supposed to be %15.8f" and
        // "47.65734000" there are 15 spaces
        String str3 = String.format("My answer is %15.8f",
                                    47.65734);
  
        // Print and display strings
        System.out.println(s);
        System.out.println(str2);
        System.out.println(str3);
    }
}


java
// Java program to demonstrate Concatenation of Arguments
// to the string using format() method
  
// Main class
class GFG {
  
    // Main driver method
    public static void main(String args[])
    {
        // Custom input string to be formatted
        String str1 = "GFG";
        String str2 = "GeeksforGeeks";
  
        // %1$ represents first argument
        // %2$ second argument
        String str = String.format(
            "My Company name"
                + " is: %1$s, %1$s and %2$s",
            str1, str2);
  
        // Print and display the formatted string
        System.out.println(str);
    }
}


java
// Java program to Illustrate Left Padding
// using format() method
  
// Main class
class GFG {
  
    // Main driver method
    public static void main(String args[])
    {
        // Custom integer number
        int num = 7044;
  
        // Output is 3 zero's("000") + "7044",
        // in total 7 digits
        String str = String.format("%07d", num);
  
        // Print and display the formatted string
        System.out.println(str);
    }
}


输出
My Company name is GeeksforGeeks
My answer is 47.65734000
My answer is     47.65734000

示例 2:

Java

// Java program to demonstrate Concatenation of Arguments
// to the string using format() method
  
// Main class
class GFG {
  
    // Main driver method
    public static void main(String args[])
    {
        // Custom input string to be formatted
        String str1 = "GFG";
        String str2 = "GeeksforGeeks";
  
        // %1$ represents first argument
        // %2$ second argument
        String str = String.format(
            "My Company name"
                + " is: %1$s, %1$s and %2$s",
            str1, str2);
  
        // Print and display the formatted string
        System.out.println(str);
    }
}
输出:
My Company name is: GFG, GFG and GeeksforGeeks

示例 3:

Java

// Java program to Illustrate Left Padding
// using format() method
  
// Main class
class GFG {
  
    // Main driver method
    public static void main(String args[])
    {
        // Custom integer number
        int num = 7044;
  
        // Output is 3 zero's("000") + "7044",
        // in total 7 digits
        String str = String.format("%07d", num);
  
        // Print and display the formatted string
        System.out.println(str);
    }
}
输出:
0007044