c9461753405df2b87585d74e5ce3e51a487e3a01.svn-base
2.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package com.thinkgem.jeesite.common.utils;
import java.util.Map;
import org.springframework.util.StringUtils;
import com.google.common.collect.Maps;
public class StringUtil extends StringUtils{
public static String getValueByMap(Map map,String key,String value){
return map.get(key)==null?value:map.get(key).toString();
}
/**
* 替换
* @param <V>
* @param source 源内容
* @param parameter 占位符参数
* @param prefix 占位符前缀 例如:${
* @param suffix 占位符后缀 例如:}
* @param enableSubstitutionInVariables 是否在变量名称中进行替换 例如:${system-${版本}}
*
String template1 = "${name} is at the age of ${age} xx ${system}";
Map<String, String> replaceValue = Maps.newHashMap();
replaceValue.put("name", "john");
replaceValue.put("age", "27");
replaceValue.put("version", "21");
replaceValue.put("system", "windows-${version}");
String param3 = StringUtil.replace(template1,replaceValue,false);
System.out.println("-------------------param3=" + param3);
String param2 = StringUtil.replace(template1,replaceValue,true);
System.out.println("-------------------param2=" + param2);
-------------------param3=john is at the age of 27 xx windows-21
-------------------param2=john is at the age of 27 xx windows-${version}
*
* 转义符默认为'$'。如果这个字符放在一个变量引用之前,这个引用将被忽略,不会被替换 如$${a}将直接输出${a}
* @return
*/
/* public static <V> String replace(String source,Map<String, V> parameter,boolean enableSubstitutionInVariables){
String prefix = "${",suffix ="}";
//StrSubstitutor不是线程安全的类
StringSubstitutor strSubstitutor = new StringSubstitutor(parameter)
.setEnableSubstitutionInVariables(enableSubstitutionInVariables)
.setDisableSubstitutionInValues(enableSubstitutionInVariables);
return strSubstitutor.replace(source);
}*/
/*public static <V> String replace(String source,Map<String, V> parameter){
String prefix = "${",suffix ="}";
//StrSubstitutor不是线程安全的类
StringSubstitutor strSubstitutor = new StringSubstitutor(parameter)
.setEnableSubstitutionInVariables(false)
.setDisableSubstitutionInValues(false);
return strSubstitutor.replace(source);
}*/
}