當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Java MessageFormat format()方法用法及代碼示例


java.text.MessageFormat類的format()方法用於獲取附加到字符串緩衝區對象中的對象的格式化數組。格式化的數組將包含所有形式的元素,位於MessageFormat對象的模式中。

用法:

public final StringBuffer format(Object[] arguments,
                                 StringBuffer result,
                                 FieldPosition pos)

參數



  • aragument:-此方法將數組對象作為要進行格式化的參數。
  • result:-字符串緩衝區將用於附加格式化的數組。
  • pos:-字段位置將用於對齊目的。

返回值:此方法返回字符串緩衝區,該緩衝區將具有格式化數組的附加結果。

異常:如果結果為null,則此方法引發NullPointerException。

下麵是說明format()方法的示例:

範例1:

// Java program to demonstrate 
// format() method 
  
import java.text.*; 
import java.util.*; 
import java.io.*; 
  
public class GFG { 
    public static void main(String[] argv) 
    { 
        try { 
            // creating and initializing new MessageFormat Object 
            MessageFormat mf 
                = new MessageFormat("{0, number, #}, {0, number, #.##}, {0, number}"); 
  
            // Creating and initializing new FieldPosition Object 
            FieldPosition fp 
                = new FieldPosition(MessageFormat.Field.ARGUMENT); 
  
            // Creating and initializing an array of type Double 
            // to be formated 
            Object[] objs = { new Double(9.5678) }; 
  
            // Creating and initializing StringBuffer for 
            // appending the result 
            StringBuffer stb = new StringBuffer(10); 
  
            // Formating an array of object 
            // using format() method 
            stb = mf.format(objs, stb, fp); 
  
            // display the result 
            System.out.println("formated array:"
                               + stb.toString()); 
        } 
        catch (NullPointerException e) { 
  
            System.out.println("Exception thrown:" + e); 
        } 
    } 
}
輸出:
formated array:10, 9.57, 9.568

範例2:

// Java program to demonstrate 
// format() method 
  
import java.text.*; 
import java.util.*; 
import java.io.*; 
  
public class GFG { 
    public static void main(String[] argv) 
    { 
        try { 
            // creating and initializing new MessageFormat Object 
            MessageFormat mf 
                = new MessageFormat("{0, number, #}, {0, number, #.##}, {0, number}"); 
  
            // Creating and initializing new FieldPosition Object 
            FieldPosition fp 
                = new FieldPosition(MessageFormat.Field.ARGUMENT); 
  
            // Creating and initializing an array of type Double 
            // to be formated 
            Object[] objs = { new Double(9.5678) }; 
  
            // Creating and initializing StringBuffer for 
            // appending the result 
            StringBuffer stb = new StringBuffer(10); 
  
            // Formating an array of object 
            // using format() method 
            stb = mf.format(objs, null, fp); 
  
            // display the result 
            System.out.println("formated array:"
                               + stb.toString()); 
        } 
        catch (NullPointerException e) { 
            System.out.println("StringBuffer is null " + e); 
            System.out.println("Exception thrown:" + e); 
        } 
    } 
}
輸出:
old pattern:{0, date, #}, {1, date, #}, {0, number}

String is Null
StringBuffer is null java.lang.NullPointerException
Exception thrown:java.lang.NullPointerException

參考: https://docs.oracle.com/javase/9/docs/api/java/text/MessageFormat.html#format-java.lang.Object:A-java.lang.StringBuffer-java.text.FieldPosition-




相關用法


注:本文由純淨天空篩選整理自RohitPrasad3大神的英文原創作品 MessageFormat format() method in Java with Example : Set – 1。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。