「Java」java.text.MessageFormatで文字列をフォーマットする方法

説明
String java.text.MessageFormat.format(String pattern, Object… arguments)
指定されたパターンを使ってMessageFormatを作成し、それを使用して指定された引数をフォーマットします。

パラメータ:
pattern – パターン文字列
arguments – フォーマットするオブジェクト

戻り値:
フォーマットされた文字列

Javaコード

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
package com.arkgame.study;
import java.text.MessageFormat;
public class MessageDemo {
public static void main(String[] args) {
// {0} {1}置換
System.out.println(MessageFormat.format("study {0} and become {1}", "skill", "smart"));
// 順番入れ替え
System.out.println(MessageFormat.format("{0},{2},{1}", "大崎", "大井町", "品川"));
// シングルオート表示
System.out.println(MessageFormat.format("''{0}''", "study in arkgame.com"));
// 数値をカンマが付いて置換
System.out.println(MessageFormat.format("'{0}',{0}", 1234));
}
}
package com.arkgame.study; import java.text.MessageFormat; public class MessageDemo { public static void main(String[] args) { // {0} {1}置換 System.out.println(MessageFormat.format("study {0} and become {1}", "skill", "smart")); // 順番入れ替え System.out.println(MessageFormat.format("{0},{2},{1}", "大崎", "大井町", "品川")); // シングルオート表示 System.out.println(MessageFormat.format("''{0}''", "study in arkgame.com")); // 数値をカンマが付いて置換 System.out.println(MessageFormat.format("'{0}',{0}", 1234)); } }
package com.arkgame.study;

import java.text.MessageFormat;

public class MessageDemo {

      public static void main(String[] args) {
            // {0} {1}置換
            System.out.println(MessageFormat.format("study {0} and become {1}", "skill", "smart"));
            // 順番入れ替え
            System.out.println(MessageFormat.format("{0},{2},{1}", "大崎", "大井町", "品川"));
            // シングルオート表示
            System.out.println(MessageFormat.format("''{0}''", "study in arkgame.com"));
            // 数値をカンマが付いて置換
            System.out.println(MessageFormat.format("'{0}',{0}", 1234));
      }
}

実行結果
study skill and become smart
大崎,品川,大井町
'study in arkgame.com’
{0},1,234

Java

Posted by arkgame