MessageFormat 動態文字

2021-07-03 14:58:38 字數 1535 閱讀 9749

messageformat:如果一段字串中有多個對地區敏感的資訊,可以使用此類進行批量國際化設定

(1)messageformat類允許開發人員用佔位符…替換掉字串中的敏感資料(即國際化相關的資料)。

(2)messageformat類在格式化輸出包含佔位符的文字時,messageformat類可以接收乙個引數陣列,以替換文字中的每乙個佔位符。

(3)佔位符有三種方式書寫方式:

: 0-9 之間的數字,表示要格式化物件資料在引數陣列中的索引號

: 引數的格式化型別

: 格式化的樣式,它的值必須是與格式化型別相匹配的合法模式、或表示合法模式的字串。

formattype:

number

date

time

choice

fomatstyle

short

medium

long

full

integer

currency

percent

subformatpattern

(4)例子

//模式字串

//at 12:30 pm on jul 3,1998, a hurricance destroyed 99 houses and caused $1000000 of damage

@test

public void demo6(), a hurricance destroyed houses and caused of damage.";

calendar calendar = calendar.getinstance();

calendar.set(1998, 6, 3, 12, 30, 0);

date date = calendar.gettime();

dateformat format = dateformat.getdatetimeinstance(dateformat.long, dateformat.short, locale.china);

string datestr = format.format(date);

object objs = ;

string result = messageformat.format(str,objs);

system.out.println(result);

}@test

public void test(), a hurricance destroyed houses and caused of damage";

messageformat format = new messageformat(msg,locale.china);

calendar  c = calendar.getinstance();

c.set(1998, 6, 3);

date date = c.gettime();

object objs = ;

string str = format.format(objs);

system.out.println(str);

}

MessageFormat(動態文字)

messageformat 動態文字 如果乙個字串中包含了多個與國際化相關的資料,可以使用messageformat 類對這些資料進行批量處理 messageformat類如何驚醒批量處理呢?messageformat類允許開發人員用佔位符替換掉字串中的敏感資料 即國際化相關的資料 messagef...

MessageFormat 例項化用法

messageformat用來格式化乙個訊息,通常是乙個字串 messageformat模式 主要部分 formatelement formattype number date time choice 需要使用choiceformat formatstyle short medium long fu...

使用MessageFormat替換字元中的佔位符

使用string.format可以實現字串的格式化功能,即將後面引數中的值替換掉format中的 s,d這些值。但messageformat更為強大,不用管傳入值是字串還是數字,使用佔位符即可。如 string format 你好,歡迎再次光臨!今天是你第次進入。string str message...