StringUtil.java 2.4 KB
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);
    }*/
}