「Java」MessageFormat.format()で文字列フォーマットを利用するサンプル
サンプルコード
package com.arkgame.study;
import java.text.MessageFormat;
import java.util.Date;
public class MessageFormatDemo {
public static void main(String[] args) {
String rule1 = "{0}を入力してください。例「{1}」";
Object[] ots1 = new Object[] { "会社名", "太郎会社" };
String msg1 = MessageFormat.format(rule1, ots1);
System.out.println(msg1);
String rule2 = "日付= {0,date},数字= {1,number,#.###}";
Object[] ots2 = { new Date(), new Float(456.7899) };
String msg2 = MessageFormat.format(rule2, ots2);
System.out.println(msg2);
}
}
実行結果
会社名を入力してください。例「太郎会社」
日付= 2020/05/13,数字= 456.79