码迷,mamicode.com
首页 > 编程语言 > 详细

两种java中的占位符的使用

时间:2019-02-16 15:07:37      阅读:710      评论:0      收藏:0      [点我收藏+]

标签:system   url   占位符   使用   输出   mes   ati   test   ESS   

第一种:使用%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中的占位符的使用

标签:system   url   占位符   使用   输出   mes   ati   test   ESS   

原文地址:https://www.cnblogs.com/buwei/p/10387734.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!