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

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

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

Java.text.MessageFormat类的format()方法用于根据消息格式对象的指定模式获取对象的格式化数组。执行操作时将考虑新的字符串模式。
句法:

public static String format(String pattern,
                            Object... arguments)

参数:此方法将以下参数作为参数。

  • pattern : –字符串模式,根据将格式化的对象数组
  • arguments :-将在其上进行格式化的对象数组。

返回值:此方法返回字符串值,该值将具有字符串格式的格式化对象数组。
异常:如果模式为空,此方法将抛出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 an array of type Double
            // to be formatted
            Object[] objs = { new Double(4.234567) };
 
            // Formatting an array of object
            // using format() method
            String str = mf.format("{0, number, #.#}", objs);
 
            // display the result
            System.out.println("formatted array : "
                               + str);
        }
        catch (NullPointerException e) {
            System.out.println("pattern is null " + 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 an array of type Double
            // to be formatted
            Object[] objs = { new Double(4.234567) };
 
            // Formatting an array of object
            // using format() method
            String str = mf.format(null, objs);
 
            // display the result
            System.out.println("formatted array : "
                               + str);
        }
        catch (NullPointerException e) {
            System.out.println("pattern is null ");
            System.out.println("Exception thrown : " + e);
        }
    }
}


输出:
formatted array : 4.2

示例 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 an array of type Double
            // to be formatted
            Object[] objs = { new Double(4.234567) };
 
            // Formatting an array of object
            // using format() method
            String str = mf.format(null, objs);
 
            // display the result
            System.out.println("formatted array : "
                               + str);
        }
        catch (NullPointerException e) {
            System.out.println("pattern is null ");
            System.out.println("Exception thrown : " + e);
        }
    }
}
输出:
pattern is null 
Exception thrown : java.lang.NullPointerException

参考: https://docs.oracle.com/javase/9/docs/api/ Java/text/MessageFormat.html#format-java.lang.String-java.lang.Object…-