Skip to content

Commit

Permalink
Add method format of map
Browse files Browse the repository at this point in the history
  • Loading branch information
looly committed Sep 9, 2014
1 parent 752812b commit a5109b3
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/main/java/com/xiaoleilu/hutool/StrUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

/**
* 字符串工具类
Expand Down Expand Up @@ -509,6 +512,23 @@ public static String format(String template, Object... values) {
return sb.toString();
}

/**
* 格式化文本
* @param template 文本模板,被替换的部分用 {key} 表示
* @param map 参数值对
* @return 格式化后的文本
*/
public static String format(String template, Map<String, Object> map) {
if(null == map || map.isEmpty()) {
return template;
}

for (Entry<String, Object> entry : map.entrySet()) {
template.replace("{" + entry.getKey() + "}", entry.getValue().toString());
}
return template;
}

/**
* 编码字符串
* @param str 字符串
Expand Down

0 comments on commit a5109b3

Please sign in to comment.