当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。