Java - String format() 方法



描述

Java String format() 方法用於使用指定的區域設定、格式字串和物件引數獲取格式化的字串。如果在 String.format() 方法中未指定區域設定,則透過呼叫 Locale.getDefault() 方法本身使用預設區域設定。

在 Java 中,區域設定是一個表示特定地理、政治或文化區域的物件。format() 方法接受不同的引數,例如區域設定、格式和物件,這些引數分別儲存區域設定 l、字串格式和物件引數的值。

format() 方法有兩個多型變體,具有不同的引數,如下面的語法所示。

語法

以下是 Java String format() 方法的語法 -

public static String format(Locale l, String format, Object... args) //first syntax
public static String format(String format, Object... args) // second syntax

引數

  • l - 這是在格式化期間要應用的區域設定。如果為 null,則不應用任何本地化。

  • format - 這是一個格式字串。

  • args - 這是格式字串中格式說明符引用的引數。如果引數多於格式說明符,則會忽略多餘的引數。引數的數量是可變的,可以為零。

返回值

此方法使用指定的區域設定、格式字串和引數返回格式化的字串。

使用區域設定獲取格式化字串示例

如果我們將 區域設定、格式和物件引數作為引數傳遞給此方法,它將返回一個格式化的字串。

在下面的程式中,我們使用值 "Java Programming" 例項化字串類。然後,使用 format() 方法,我們嘗試在指定的區域設定 "Locale.CANADA" 和物件引數處格式化當前字串。

package com.tutorialspoint;

import java.util.Locale;

public class Format {
   public static void main(String[] args) {
     
      //instantiate the string class
      String str = new String("Ramesh");
      System.out.println("The given string is: " + str);
      System.out.println("After formatting the string.....\n" 
         + String.format(Locale.CANADA, "My name is: %s", str));
   }
}

輸出

執行上述程式後,將產生以下結果 -

The given string is: Ramesh
After formatting the string....
My name is: Ramesh

格式化字串示例

如果我們將 格式和物件引數作為引數傳遞給方法,則此方法將返回一個格式化的字串。

在下面的示例中,我們使用值 "TutorialsPoint" 建立字串類的物件。使用 format() 方法,我們嘗試獲取當前字串在指定格式中的格式化字串。

package com.tutorialspoint;

import java.util.Locale;

public class Format {
   public static void main(String[] args) {
      
      // create an object of the string class
      String str = new String("TutorialsPoint");
      
      // initialize the double variable
      Double d = 234.454d;
      System.out.println("The given string is: " + str);
      System.out.println("The given double value is: " + d);
      
      // use format() method
      String new_str;
      String new_str1;
      new_str = String.format("Welcome to the %s", str);
      new_str1 = String.format("The result is: %15f", d);
      System.out.println("The formatted string is: " + new_str);
      System.out.println("The formatted value is: " + new_str1);
   }
}

輸出

以下是上述程式的輸出 -

The given string is: TutorialsPoint
The given double value is: 234.454
The formatted string is: Welcome to the TutorialsPoint
The formatted value is: The result is:     234.454000

檢查格式化字串時出現的 NullPointerException 示例

如果格式值為 null,則 format() 方法將返回 NullPointerException

在此示例中,我們使用值 "Hello" 建立字串類的例項。然後,使用 format() 方法,我們嘗試獲取當前字串在指定格式 null 中的格式化字串。

package com.tutorialspoint;

import java.util.Locale;

public class Format {
   public static void main(String[] args) {
      try {
         
         // create a string literal
         String str1 = new String("Hello");
         System.out.println("The given string is: " + str1);
         
         // using format() method
         String new_str = String.format(Locale.CANADA, null, str1);
      } catch(Exception e) {
         e.printStackTrace();
         System.out.println("Exception: " + e);
      }
   }
}

輸出

上述程式產生以下結果 -

The given string is: Hello
java.lang.NullPointerException: Cannot invoke "String.length()" because "s" is null
	at java.base/java.util.Formatter.parse(Formatter.java:2717)
	at java.base/java.util.Formatter.format(Formatter.java:2671)
	at java.base/java.util.Formatter.format(Formatter.java:2625)
	at java.base/java.lang.String.format(String.java:4184)
	at com.tutorialspoint.String.Format.main(Format.java:11)
Exception: java.lang.NullPointerException: Cannot invoke "String.length()" because "s" is null

檢查格式化字串時出現的 IllegalFormatException 示例

如果我們將 非法格式語法傳遞給方法,它將丟擲 IllegalFormatException。

在此程式中,我們使用值 "Tutorix" 建立字串類的物件。然後,使用 format() 方法,我們嘗試獲取當前字串物件在指定格式 "%d" 中的格式化字串。由於我們對字串使用 "%d" 格式,因此該方法將丟擲異常。

package com.tutorialspoint;

import java.util.Locale;

public class Format {
   public static void main(String[] args) {
      try {
         
         // create an object of the string class
         String str = new String("Tutorix");
         System.out.println("The given string is: " + str);
         
         // using format method we pass
         String new_string = String.format("%d", str);
         System.out.println("The new formatted string is: " + new_string);
      } catch(Exception e) {
         e.printStackTrace();
         System.out.println("Exception: " + e);
      }
   }
}

輸出

執行上述程式後,將產生以下輸出 -

The given string is: Tutorix
java.util.IllegalFormatConversionException: d != java.lang.String
	at java.base/java.util.Formatter$FormatSpecifier.failConversion(Formatter.java:4442)
	at java.base/java.util.Formatter$FormatSpecifier.printInteger(Formatter.java:2963)
	at java.base/java.util.Formatter$FormatSpecifier.print(Formatter.java:2918)
	at java.base/java.util.Formatter.format(Formatter.java:2689)
	at java.base/java.util.Formatter.format(Formatter.java:2625)
	at java.base/java.lang.String.format(String.java:4143)
	at com.tutorialspoint.String.Format.main(Format.java:10)
Exception: java.util.IllegalFormatConversionException: d != java.lang.String
java_lang_string.htm
廣告