Java Formatter format() 方法



描述

Java Formatter format(Locale l,String format,Object... args) 方法使用指定的區域設定、格式字串和引數將格式化的字串寫入此物件的目的地。

宣告

以下是 java.util.Formatter.format() 方法的宣告

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

引數

  • l − 格式化期間要應用的區域設定。如果 l 為 null,則不應用任何本地化。這不會更改在構造期間設定的此物件的區域設定。

  • format − 格式字串,如格式字串語法中所述。

  • args − 格式字串中格式說明符引用的引數。如果引數多於格式說明符,則忽略多餘的引數。引數的最大數量受 Java 虛擬機器規範定義的 Java 陣列的最大維度限制。

返回值

此方法返回此格式化程式。

異常

  • FormatterClosedException − 如果此格式化程式已透過呼叫其 close() 方法關閉。

  • IllegalFormatException − 如果格式字串包含非法的語法、與給定引數不相容的格式說明符、給定格式字串的引數不足或其他非法條件。

使用 Formatter 示例格式化包含變數的字串

以下示例演示瞭如何使用 Java Formatter format(Locale, String, Object) 方法使用格式化程式格式化字串。我們建立了一個帶有 StringBuffer 和區域設定的格式化程式物件。Formatter 用於使用 format() 方法列印字串。

package com.tutorialspoint;

import java.util.Formatter;
import java.util.Locale;

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

      // create a new formatter
      StringBuffer buffer = new StringBuffer();
      Formatter formatter = new Formatter(buffer, Locale.US);

      // format a new string
      String name = "World";
      formatter.format(Locale.US, "Hello %s !", name);

      // print the formatted string 
      System.out.println(formatter);
	  
      formatter.close();
   }
}

輸出

讓我們編譯並執行上述程式,這將產生以下結果:

Hello World !

使用 Formatter 示例格式化包含兩個變數的字串

以下示例演示瞭如何使用 Java Formatter format(Locale, String, Object, Object) 方法使用格式化程式格式化字串。我們建立了一個帶有 StringBuffer 和區域設定的格式化程式物件。Formatter 用於使用 format() 方法列印字串。

package com.tutorialspoint;

import java.util.Formatter;
import java.util.Locale;

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

      // create a new formatter
      StringBuffer buffer = new StringBuffer();
      Formatter formatter = new Formatter(buffer, Locale.US);

      // format a new string
      String name = "Robert";
      int rollNo = 10;
      formatter.format(Locale.US, "Name = %s, RollNo = %d", name, rollNo);

      // print the formatted string 
      System.out.println(formatter);
      
      formatter.close();
   }
}

輸出

讓我們編譯並執行上述程式,這將產生以下結果:

Name = Robert, RollNo = 10

使用 Formatter 示例格式化包含三個變數的字串

以下示例演示瞭如何使用 Java Formatter format(Locale, String, Object, Object, Object) 方法使用格式化程式格式化字串。我們建立了一個帶有 StringBuffer 和區域設定的格式化程式物件。Formatter 用於使用 format() 方法列印字串。

package com.tutorialspoint;

import java.util.Formatter;
import java.util.Locale;

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

      // create a new formatter
      StringBuffer buffer = new StringBuffer();
      Formatter formatter = new Formatter(buffer, Locale.US);

      // format a new string
      String name = "Robert";
      int rollNo = 10;
      String section = "A";
      formatter.format(Locale.US,"Name = %s, RollNo = %d, Section = %s", name, rollNo, section);

      // print the formatted string 
      System.out.println(formatter);
      
      formatter.close();
   }
}

輸出

讓我們編譯並執行上述程式,這將產生以下結果:

Name = Robert, RollNo = 10, Section = A
java_util_formatter.htm
廣告

© . All rights reserved.