正则表达式 - Java 字符串中{ }占位符替换方面的问题
阿神
阿神 2017-04-18 10:23:36
0
3
889

怎么使用map中的key,替换字符串中{}中的占位符? 代码如下,请大神赐教……

     String str = "我是{name},我来自{city},今年{age}岁";
        Map<String, String> mapstring = new HashMap<String, String>();
        mapstring.put("name", "小明");
        mapstring.put("age", "15");
        mapstring.put("city", "北京");        
        
        ……………………
        
        System.out.println("我是小明,我来自北京,今年15岁");
阿神
阿神

闭关修行中......

reply all(3)
小葫芦

Not sure why there is such a requirement, but the following code should meet the requirement:

public class Test {

    public static void main(String[] args) throws Exception {
        String str = "我是{name},我来自{city},今年{age}岁";
        Map<String, String> mapstring = new HashMap<>();
        mapstring.put("name", "小明");
        mapstring.put("age", "15");
        mapstring.put("city", "北京");

        for (Map.Entry<String, String> entry : mapstring.entrySet()) {
            str = str.replace("{" + entry.getKey() + "}", entry.getValue());
        }

        System.out.println("我是小明,我来自北京,今年15岁");
        System.out.println(str);
    }
}
伊谢尔伦

Just finished writing the answer, the correct answer is upstairs
String a = "I am {name}, I come from {city}, I am {age} years old this year";

    Map<String, String> mapstring = new HashMap<String, String>();
    mapstring.put("name", "小明");
    mapstring.put("age", "15");
    mapstring.put("city", "北京"); 
    if(!mapstring.isEmpty()&&mapstring!=null){
        for (Map.Entry<String, String> entry : mapstring.entrySet()) {
            a = a.replace("{"+entry.getKey()+"}", entry.getValue());
        }
    }
黄舟

Haha I will give you a tool class that I packaged
/**

  • String formatting

  • @author Du

  • */

public class DFormat {

private static final Pattern pattern = Pattern.compile("\{(.*?)\}");
private static Matcher matcher;

/**
 * 格式化字符串 字符串中使用{key}表示占位符
 * 
 * @param sourStr
 *            需要匹配的字符串
 * @param param
 *            参数集
 * @return
 */
public static String stringFormat(String sourStr, Map<String, Object> param) {
    String tagerStr = sourStr;
    if (param == null)
        return tagerStr;
    matcher = pattern.matcher(tagerStr);
    while (matcher.find()) {
        String key = matcher.group();
        String keyclone = key.substring(1, key.length() - 1).trim();
        Object value = param.get(keyclone);
        if (value != null)
            tagerStr = tagerStr.replace(key, value.toString());
    }
    return tagerStr;
}

/**
 * 格式化字符串 字符串中使用{key}表示占位符 利用反射 自动获取对象属性值 (必须有get方法)
 * 
 * @param sourStr
 *            需要匹配的字符串
 * @param param
 *            参数集
 * @return
 */
public static String stringFormat(String sourStr, Object obj) {
    String tagerStr = sourStr;
    matcher = pattern.matcher(tagerStr);
    if (obj == null)
        return tagerStr;

    PropertyDescriptor pd;
    Method getMethod;
    // 匹配{}中间的内容 包括括号
    while (matcher.find()) {
        String key = matcher.group();
        String keyclone = key.substring(1, key.length() - 1).trim();
        try {
            pd = new PropertyDescriptor(keyclone, obj.getClass());
            getMethod = pd.getReadMethod();// 获得get方法
            Object value = getMethod.invoke(obj);
            if (value != null)
                tagerStr = tagerStr.replace(key, value.toString());
        } catch (Exception e) {
            // TODO Auto-generated catch block
            // Loggers.addException(e);
        }
    }
    return tagerStr;
}

/**
 * 格式化字符串 (替换所有) 字符串中使用{key}表示占位符
 * 
 * @param sourStr
 *            需要匹配的字符串
 * @param param
 *            参数集
 * @return
 */
public static String stringFormatAll(String sourStr, Map<String, Object> param) {
    String tagerStr = sourStr;
    if (param == null)
        return tagerStr;
    matcher = pattern.matcher(tagerStr);
    while (matcher.find()) {
        String key = matcher.group();
        String keyclone = key.substring(1, key.length() - 1).trim();
        Object value = param.get(keyclone);
        if (value != null)
            tagerStr = tagerStr.replace(key, value.toString());
    }
    return tagerStr;
}

}

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template