How to create dynamic Template String

前端 未结 4 1649
北荒
北荒 2020-12-22 14:06

Is there is any API which replace template string along with values using Spring or java.

For example:

Dear %FIRST_NAME% %LAST_NAME%,
---- remaining          


        
相关标签:
4条回答
  • 2020-12-22 14:24

    It's relatively simple to write code that will do this. If this is something you're going to be doing a lot however you might want to look into using an existing library like Velocity. It uses a different syntax for values however. See Getting Started.

    If you do want to write this yourself try:

    public static String replaceAll(String text, Map<String, String> params) {
      return replaceAll(text, params, '%', '%');
    }
    
    public static String replaceAll(String text, Map<String, String> params,
        char leading, char trailing) {
      String pattern = "";
      if (leading != 0) {
        pattern += leading;
      }
      pattern += "(\\w+)";
      if (trailing != 0) {
        pattern += trailing;
      }
      Pattern p = Pattern.compile(pattern);
      Matcher m = p.matcher(text);
      boolean result = m.find();
      if (result) {
        StringBuffer sb = new StringBuffer();
        do {
          String replacement = params.get(m.group(1));
          if (replacement == null) {
            replacement = m.group();
          }
          m.appendReplacement(sb, replacement);
          result = m.find();
        } while (result);
        m.appendTail(sb);
        return sb.toString();
      }
      return text;
    }
    

    For example:

    String in = "Hi %FIRST_NAME% %LAST_NAME%.";
    Map<String, String> params = new HashMap<String, String>();
    params.put("FIRST_NAME", "John");
    params.put("LAST_NAME", "Smith");
    String out = replaceAll(in, params);
    System.out.println(out);
    

    Output:

    Hi John Smith.
    
    0 讨论(0)
  • 2020-12-22 14:31

    Check StringTemplate, http://www.stringtemplate.org/ and article here to get feel of it, http://www.codecapers.com/post/Generating-Structured-Text-with-StringTemplate.aspx

    0 讨论(0)
  • 2020-12-22 14:41

    My favorite templating engine is Apache Velocity

    Integrates nicely with Spring as well, theres an introductory article here

    0 讨论(0)
  • 2020-12-22 14:44

    Usage:

    replaceAll(string,
            "param1", "value 1",
            "param12", "value 12");
    

    Implementation:

    public static String replaceAll(String text, Object... replacements) {
        Assert.isTrue(replacements.length % 2 == 0, "Is not key - value pairs: " + replacements);
    
        Map< String, String> longToShortNamesMap = new TreeMap<>(new Comparator<String>() {
            @Override
            public int compare(String o1, String o2) {
                return o1.length() == o2.length() ? o1.compareTo(o2) : o2.length() - o1.length();
            }
        });
        for (int i = 0; i < replacements.length; i += 2) {
            longToShortNamesMap.put(Objects.toString(replacements[i]), Objects.toString(replacements[i+1]));
        }
        String result = text;
        for (String key : longToShortNamesMap.keySet()) {
            Assert.isTrue(text.contains(key), "Can not find key in the text " + key);
            result = result.replaceAll(key, longToShortNamesMap.get(key));
        }
        return result;
    }
    
    0 讨论(0)
提交回复
热议问题