【问题标题】:Python string 'Template' equivalent in javajava中的Python字符串“模板”等价物
【发布时间】:2014-08-23 10:06:00
【问题描述】:

Python 支持以下操作:

>>> s = Template('$who likes $what')
>>> s.substitute(who='tim', what='kung pao')
'tim likes kung pao'

(示例取自 Python 官方documentation

在 Java 中是否有等效的方法来执行确切的任务?

谢谢

【问题讨论】:

    标签: java python string equivalent


    【解决方案1】:

    看看http://www.stringtemplate.org/。这是一个例子:

    ST hello = new ST("Hello, <name>");
    hello.add("name", "World");
    System.out.println(hello.render());
    

    打印出来:

    "Hello World"
    

    【讨论】:

    【解决方案2】:

    块模板 (http://www.x5dev.com/chunk) 让这种事情变得非常简单:

    Chunk c = new Chunk();
    c.append("{$who} likes {$what}");
    c.set("who", "tim");
    c.set("what", "kung pao");
    String output = c.toString();
    

    或者如果你已经有Map&lt;String,String&gt;

    Chunk c = new Chunk();
    c.append("{$who} likes {$what}");
    Map<String,String> tagValues = getTagValues();
    c.setMultiple(tagValues);
    c.render(System.out);
    

    Chunk 还可以轻松地从一个文件或一组文件中加载模板,并支持循环、分支和演示过滤器。

    【讨论】:

    【解决方案3】:

    我不知道是否有任何平等,但你可以这样做:

    String s = "$who likes $what";
    s.replace("$who", "tim").replace("$what", "kung pao");
    

    你会得到同样的结果。

    【讨论】:

      【解决方案4】:

      here 回答了另一个选项。为方便起见,将重复一遍。

      有一个库org.apache.commons:commons-text:1.9,类StringSubstitutor。这就是它的工作原理:

       // Build map
       Map<String, String> valuesMap = new HashMap<>();
       valuesMap.put("animal", "quick brown fox");
       valuesMap.put("target", "lazy dog");
       String templateString = "The ${animal} jumped over the ${target}.";
      
       // Build StringSubstitutor
       StringSubstitutor sub = new StringSubstitutor(valuesMap);
      
       // Replace
       String resolvedString = sub.replace(templateString);
      

      还是有一句话。 StringSubstitutor 实例是使用替换映射创建的,然后使用其 replace 方法解析模板字符串。这意味着它无法预先解析模板字符串,因此使用不同的替换映射处理相同的模板可能效率较低。

      Python 的string.Template 以相反的方式工作。它是使用模板字符串创建的,然后使用其substitutesafe_substitute 方法处理替换映射。所以理论上它可以预先解析模板字符串,这可能会带来一些性能提升。

      Python 的 string.Template 也将默认处理 ether $variable${variable}。到目前为止找不到如何调整 StringSubstitutor 来做到这一点。

      默认情况下StringSubstitutor 会解析可能导致无限循环的值中的占位符。 stringSubstitutor.setDisableSubstitutionInValues(true) 将禁用此行为。

      【讨论】:

        【解决方案5】:

        String s = String.format("%s likes %s", "tim", "kung pao");

        System.out.printf("%s likes %s", "tim", "kung pao");

        你也可以很容易地用它来做模板。

        String s = "%s likes %s";
        String.format(s, "tim", "kung pao");
        

        【讨论】:

        • 这对我没有帮助,因为我想在代码的一部分中启动模板并在其他地方进行替换。
        • 太好了,谢谢。我想这就是我想要的。
        • String.format 更像是 Python 自己的字符串格式(使用% 或类似的"string".format())。 Template 的基础是能够替换自定义命名区域。
        【解决方案6】:

        我认为你可以使用String.format 来实现这一点。

        http://javarevisited.blogspot.sk/2012/08/how-to-format-string-in-java-printf.html

        【讨论】:

          猜你喜欢
          • 2021-12-15
          • 1970-01-01
          • 2010-12-25
          • 2011-01-10
          • 1970-01-01
          • 2011-06-16
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多