用 Java 替換 String 中的標記
要在 Java 中替換 String 中的標記,我們使用 Message Format 類。Message Format 類提供了一種生成不依賴於語言的連線訊息的方法。Message Format 類擴充套件 Serializable 和 Cloneable 介面。
宣告 − java.text.MessageFormat 類宣告如下 −
public class MessageFormat extends Format
MessageFormat.format(pattern, params) 方法格式化訊息,並使用與引數號和陣列索引匹配的 params 陣列中的物件填充缺失的部分。
format 方法有兩個引數,一個模式和一個引數陣列。模式包含在 {} 大括號中的佔位符,指示將引數值儲存在陣列中的位置。
讓我們看一個用 Java 替換標記的例子 −
示例
import java.text.MessageFormat;
public class Example {
public static void main(String[] args) {
Object[] obj = new Object[] { "Good", "!!"};
// The Morning argument has the placeholders 0 and 1 so it lies between Good and !!
// Thus it substitutes !! token with Morning token
System.out.println(MessageFormat.format("{0} Morning {1}", obj));
}
}輸出
Good Morning !!
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP