📌  相关文章
📜  Java中的 MessageFormat format() 方法示例:Set – 1

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

Java中的 MessageFormat format() 方法示例:Set – 1

Java.text.MessageFormat类的format()方法用于获取附加到字符串缓冲区对象的格式化对象数组。格式化数组将包含位于 MessageFormat 对象模式中的所有形式的元素。
句法:

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

参数

  • argument : – 此方法将数组对象作为参数,将对其进行格式化。
  • 结果:-字符串缓冲区将用于附加格式化数组。
  • pos :-字段位置将用于对齐目的。

返回值:此方法返回字符串缓冲区,该缓冲区将具有格式化数组的附加结果。
异常:如果结果为 null,则此方法抛出NullPointerException
以下是说明format()方法的示例:
示例 1:

Java
// 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 formatted
            Object[] objs = { new Double(9.5678) };
 
            // Creating and initializing StringBuffer for
            // appending the result
            StringBuffer stb = new StringBuffer(10);
 
            // Formatting an array of object
            // using format() method
            stb = mf.format(objs, stb, fp);
 
            // display the result
            System.out.println("formatted array : "
                               + stb.toString());
        }
        catch (NullPointerException e) {
 
            System.out.println("Exception thrown : " + e);
        }
    }
}


Java
// 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 formatted
            Object[] objs = { new Double(9.5678) };
 
            // Creating and initializing StringBuffer for
            // appending the result
            StringBuffer stb = new StringBuffer(10);
 
            // Formatting an array of object
            // using format() method
            stb = mf.format(objs, null, fp);
 
            // display the result
            System.out.println("formatted array : "
                               + stb.toString());
        }
        catch (NullPointerException e) {
            System.out.println("StringBuffer is null " + e);
            System.out.println("Exception thrown : " + e);
        }
    }
}


示例 2:

Java

// 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 formatted
            Object[] objs = { new Double(9.5678) };
 
            // Creating and initializing StringBuffer for
            // appending the result
            StringBuffer stb = new StringBuffer(10);
 
            // Formatting an array of object
            // using format() method
            stb = mf.format(objs, null, fp);
 
            // display the result
            System.out.println("formatted 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-