1. 程式人生 > >兩種java中的占位符的使用

兩種java中的占位符的使用

sta class ring print ESS nbsp int 控制臺 pre

第一種:使用%s占位,使用String.format轉換
public class Test {
public static void main(String[] args) {
String url = "我叫%s,今年%s歲。";
String name = "小明";
String age = "28";
url = String.format(url,name,age);
System.out.println(url);
}
}
控制臺輸出: 我叫小明,年28歲。 第二種:使用{1}占位,使用MessageFormat.format轉換
public class Test {
public static void main(String[] args) {
String url02 = "我叫{0},今年{1}歲。";
String name = "小明";
String age = "28";
url02 = MessageFormat.format(url02,name,age);
System.out.println(url02);
}
}
控制臺同樣輸出:
我叫小明,今年28歲。

兩種java中的占位符的使用