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


Java MessageFormat parse()函數用法及代碼示例


java.text.MessageFormat類的parse()方法用於從parse()方法中傳遞的解析位置開始解析字符串對象。

用法:

public Object[] parse(String source,
                      ParsePosition pos)

參數:此方法采用以下參數作為參數。



  • source:-將要執行解析的字符串。
  • pos:-它是解析的起始索引。

返回值:此方法返回對象數組作為輸出。

異常:如果解析位置為null,則此方法引發NullPointerException。

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

範例1:

// Java program to demonstrate 
// parse() 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(3); 
  
            // parsing the string starting from pos 
            // accoridng to MessageFormat 
            // using parse() method 
            Object[] hash = mf.parse(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("\nParse position is Null"); 
            System.out.println("Exception thrown:" + e); 
        } 
    } 
}
輸出:
Parsed value are:
456
30.444
20.325

範例2:

// Java program to demonstrate 
// parse() 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(3); 
  
            // parsing the string starting from pos 
            // accoridng to MessageFormat 
            // using parse() method 
            Object[] hash = mf.parse(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#parse-java.lang.String-java.text.ParsePosition-




相關用法


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