• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

Java字符串格式化,{}占位符根据名字替换实例

java 搞代码 4年前 (2022-01-09) 40次浏览 已收录 0个评论

我就废话不多说了,大家还是直接看代码吧~

import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class StringFormatUtil {

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

  /**
   * 格式化字符串 字符串中使用{key}表示占位符
   *
   * @param sourStr
   *      需要匹配的字符串
   * @param param
   *  <i>本文来源gaodai$ma#com搞$代*码网2</i>    参数集
   * @return
   */
  public static String stringFormat(String sourStr, Map<String, Object> param) {
    String tagerStr = sourStr;
    if (param == null)
      return tagerStr;
    try {
      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());
      }
    }catch (Exception e){
      return null;
    }
    return tagerStr;
  }

  /**
   * 格式化字符串 字符串中使用{key}表示占位符 利用反射 自动获取对象属性值 (必须有get方法)
   *
   * @param sourStr 需要匹配的字符串
   *
   * @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;
    try {
      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());
      }
    }catch (Exception e){
      return null;
    }
    return tagerStr;
  }

  /**
   * 格式花字符串,按照占位符名字
   * 输入:sourStr = xxxxx{a}xxxx{b} ,param = {a:A,b:B}
   * 输出:targetStr = xxxxAxxxxB
   * @param sourStr
   * @param param
   * @return
   */
  public static String stringFormat(String sourStr, JSONObject param) {
    String tagerStr = sourStr;
    if (param == null)
      return tagerStr;
    try {
      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());
      }
    }catch (Exception e){
      return null;
    }
    return tagerStr;
  }

  public static void main(String[] args) {
//    Map<String,Object> map = new HashMap<>();
//    map.put("id","111");
//    map.put("sss","ss");
//    JSONObject json = new JSONObject();
//    json.put("id","212");
//    json.put("fff","xxxx");
//    json.put("emmmmm",11);
//    stringFormat("sisas&{fff}_diwahwi%{id}{jio}",json);
  }
}

搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:Java字符串格式化,{}占位符根据名字替换实例
喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址