📌  相关文章
📜  Java中的 MessageFormat parseObject() 方法与示例

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

Java中的 MessageFormat parseObject() 方法与示例

Java.text.MessageFormat类的parseObject()方法用于从 parseObject() 方法中传递的解析位置开始解析字符串对象。
句法:

public Object parseObject(String source,
                          ParsePosition pos)

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

  • source :-将执行解析的字符串。
  • pos :-它是解析的起始索引。

返回值:此方法返回对象数组作为输出。
异常:如果解析位置为空,此方法将抛出NullPointerException
以下是说明parseObject()方法的示例:
示例 1:

Java
// Java program to demonstrate
// parseObject() method
 
import java.text.*;
import java.util.*;
import java.io.*;
 
public class GFG {
    public static void main(String[] argv)
    {
        try {
            // creating and initializing  MessageFormat
            MessageFormat mf
                = new MessageFormat("{0, number, #}, {2, number, #.#}, {1, number, #.##}");
 
            // creating and initializing String source
            String str = "10.456, 20.325, 30.444";
 
            // creating and initializing ParsePosition
            ParsePosition pos = new ParsePosition(0);
 
            // parsing the string starting from pos
            // according to MessageFormat
            // using parseObject() method
            Object[] hash = (Object[])mf.parseObject(str, pos);
 
            // display the result
            System.out.println("Parsed value are :");
            for (int i = 0; i < hash.length; i++)
                System.out.println(hash[i]);
        }
        catch (NullPointerException e) {
            System.out.println("Parse position is Null");
            System.out.println("Exception thrown : " + e);
        }
    }
}


Java
// Java program to demonstrate
// parseObject() method
 
import java.text.*;
import java.util.*;
import java.io.*;
 
public class GFG {
    public static void main(String[] argv)
    {
        try {
            // creating and initializing  MessageFormat
            MessageFormat mf
                = new MessageFormat("{0, number, #}, {2, number, #.#}, {1, number, #.##}");
 
            // creating and initializing String source
            String str = "10.456, 20.325, 30.444";
 
            // creating and initializing ParsePosition
            // ParsePosition pos = new ParsePosition(0);
 
            // parsing the string starting from pos
            // according to MessageFormat
            // using parseObject() method
            Object[] hash = (Object[])mf.parseObject(str, null);
 
            // display the result
            System.out.println("Parsed value are :");
            for (int i = 0; i < hash.length; i++)
                System.out.println(hash[i]);
        }
        catch (NullPointerException e) {
            System.out.println("Parse position is Null");
            System.out.println("Exception thrown : " + e);
        }
    }
}


输出:
Parsed value are :
10.456
30.444
20.325

示例 2:

Java

// Java program to demonstrate
// parseObject() method
 
import java.text.*;
import java.util.*;
import java.io.*;
 
public class GFG {
    public static void main(String[] argv)
    {
        try {
            // creating and initializing  MessageFormat
            MessageFormat mf
                = new MessageFormat("{0, number, #}, {2, number, #.#}, {1, number, #.##}");
 
            // creating and initializing String source
            String str = "10.456, 20.325, 30.444";
 
            // creating and initializing ParsePosition
            // ParsePosition pos = new ParsePosition(0);
 
            // parsing the string starting from pos
            // according to MessageFormat
            // using parseObject() method
            Object[] hash = (Object[])mf.parseObject(str, null);
 
            // display the result
            System.out.println("Parsed value are :");
            for (int i = 0; i < hash.length; i++)
                System.out.println(hash[i]);
        }
        catch (NullPointerException e) {
            System.out.println("Parse position is Null");
            System.out.println("Exception thrown : " + e);
        }
    }
}
输出:
Parse position is Null
Exception thrown : java.lang.NullPointerException

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