Java字符串模板格式化汇总8法(附性能对⽐)Java字符串模板格式化汇总8法(附性能对⽐)
结论:
1. 循环中, 不要⽤+进⾏字符串拼接, ⽽⽤StringBuilder.append()⽅法
2. ⾮循环中, 字符串拼接使⽤+性能最⾼, 其次是StringBuilder.append()⽅法
1. ++
对于初学JAVA的蒙童,⼤约都会使⽤这招
@Test
public void testAdd(){
Date now = new Date();
String yearMonth = String(now, DatePattern.YEAR_AND_MONTH);
String expressDeliveryType = "sf";
String fileName = String(now, DatePattern.TIMESTAMP);
String template = "/home/expressdelivery/" + yearMonth + "/" + expressDeliveryType + "/vipQuery_" + fileName + ".log";
System.out.println(template);
}
输出 :
/home/expressdelivery/2017-07/sf/vipQuery_20170723042314.log
2. StringBuffer / StringBuilder
Date now = new Date();
String yearMonth = String(now, DatePattern.YEAR_AND_MONTH);
String expressDeliveryType = "sf";
String fileName = String(now, DatePattern.TIMESTAMP);
StringBuilder sb = new StringBuilder();
sb.append("/home/expressdelivery/");
sb.append(yearMonth);
sb.append("/");
sb.append(expressDeliveryType);
sb.append("/vipQuery_");
sb.append(fileName);
sb.append(".log");
String template = sb.toString();
System.out.println(template);
}
输出 :
/home/expressdelivery/2017-07/sf/vipQuery_20170723042603.log
缺点:
代码太长了
3. StringUtil.format(String, Object…)
使⽤ lang.StringUtil.format(String, Object…)
内部封装了 String.format(String, Object)
@Test
public void testStringFormat(){
Date now = new Date();
String yearMonth = String(now, DatePattern.YEAR_AND_MONTH);
String expressDeliveryType = "sf";
String fileName = String(now, DatePattern.TIMESTAMP);
String template = StringUtil.format("/home/expressdelivery/%s/%s/vipQuery_%s.log", yearMonth, expressDeliveryType, fileName);
System.out.println(template);
}
输出 :
/home/expressdelivery/2017-07/sf/vipQuery_20170723043153.log
4. MessageFormatUtil.format(String, Object…)
使⽤ ext.MessageFormatUtil.format(String, Object…)
内部封装了 MessageFormat.format(String, Object…)
Date now = new Date();
String yearMonth = String(now, DatePattern.YEAR_AND_MONTH);
String expressDeliveryType = "sf";
String fileName = String(now, DatePattern.TIMESTAMP);
String template = MessageFormatUtil
.format("/home/expressdelivery/{0}/{1}/vipQuery_{2}.log", yearMonth, expressDeliveryType, fileName);
System.out.println(template);
}
输出 :
/home/expressdelivery/2017-07/sf/vipQuery_20170723043153.log
5. Slf4jUtil.format(String, Object…)
使⽤ ls.slf4j.Slf4jUtil.format(String, Object…)
借助 slf4j ⽇志占位符
@Test
public void testSlf4jFormat(){
Date now = new Date();
String yearMonth = String(now, DatePattern.YEAR_AND_MONTH);
String expressDeliveryType = "sf";
String fileName = String(now, DatePattern.TIMESTAMP);
String template = Slf4jUtil.format("/home/expressdelivery/{}/{}/vipQuery_{}.log", yearMonth, expressDeliveryType, fileName);
System.out.println(template);
}
输出:
/home/expressdelivery/2017-07/sf/vipQuery_20170723144236.log
6. place(CharSequence, Map<String, V>)
使⽤ place(CharSequence, Map<String, V>)
内部封装了 apache commons-lang3 org.StrSubstitutor , 现在叫 commons-text
使⽤给定的字符串 templateString 作为模板,解析匹配的变量 .
public void testReplace(){
Date date = new Date();
Map<String, String> map = new HashMap<>();
map.put("yearMonth", String(date, YEAR_AND_MONTH));
map.put("expressDeliveryType", "sf");
map.put("fileName", String(date, TIMESTAMP));
String template = place("/home/expressdelivery/${yearMonth}/${expressDeliveryType}/vipQuery_${fileName}.log", map);
System.out.println(template);
}
输出:
/home/expressdelivery/2017-07/sf/vipQuery_20170723144608.log
优点:
模块可以定义变量名字了,不怕混乱
Note此⽅法只能替换字符串,⽽不能像el表达式⼀样使⽤对象属性之类的来替换
7. VelocityUtil.parseString(String, Map<String, ?>)
使⽤ ls.velocity.VelocityUtil.parseString(String, Map<String, ?>)
该⽅法需要 jar
<dependency>
<groupId>com.ls</groupId>
<artifactId>feilong-tools-velocity</artifactId>
<version>${version.feilong-platform}</version>
</dependency>
@Test
public void testVelocityParseString(){
Date date = new Date();
Map<String, String> map = new HashMap<>();
map.put("yearMonth", String(date, YEAR_AND_MONTH));
map.put("expressDeliveryType", "sf");
java valueofmap.put("fileName", String(date, TIMESTAMP));
VelocityUtil velocityUtil = new VelocityUtil();
String template = velocityUtil
.parseString("/home/expressdelivery/${yearMonth}/${expressDeliveryType}/vipQuery_${fileName}.log", map);
System.out.println(template);
}
输出
/home/expressdelivery/2017-07/sf/vipQuery_20170723145856.log
8. VelocityUtil.parseTemplateWithClasspathResourceLoader(String, Map<String, ?>)使⽤ ls.velocity.VelocityUtil.parseTemplateWithClasspathResourceLoader(String, Map<Stri
ng, ?>)
该⽅法需要 jar
<dependency>
<groupId>com.ls</groupId>
<artifactId>feilong-tools-velocity</artifactId>
<version>${version.feilong-platform}</version>
</dependency>
该⽅法适合于 字符串模板独⽴成⽂件, ⽅便维护
路径是classpath 下⾯, ⽐如 velocity/path.vm
此时代码需要如此调⽤:
@Test
public void testVelocityParseTemplateWithClasspathResourceLoader(){
Date date = new Date();
Map<String, String> map = new HashMap<>();
map.put("yearMonth", String(date, YEAR_AND_MONTH));
map.put("expressDeliveryType", "sf");
map.put("fileName", String(date, TIMESTAMP));
VelocityUtil velocityUtil = new VelocityUtil();
String template = velocityUtil.parseTemplateWithClasspathResourceLoader("velocity/path.vm", map);
System.out.println(template);
}
输出 :
/home/expressdelivery/2017-07/sf/vipQuery_20170723150443.log
9. 性能对⽐
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论