【问题标题】:Simple way templating multiline strings in java code在java代码中模板化多行字符串的简单方法
【发布时间】:2014-02-09 12:22:31
【问题描述】:

我经常遇到以下情况:我有很长的多行字符串,其中必须注入属性 - 例如类似模板的东西。但我不想在我的项目中包含一个完整的模板引擎(如velocity 或freemarker)。

如何以简单的方式做到这一点:

String title = "Princess";
String name  = "Luna";
String community = "Stackoverflow";

String text =
   "Dear " + title + " " + name + "!\n" +  
   "This is a question to " + community + "-Community\n" + 
   "for simple approach how to code with Java multiline Strings?\n" + 
   "Like this one.\n" + 
   "But it must be simple approach without using of Template-Engine-Frameworks!\n" + 
   "\n" + 
   "Thx for ..."; 

【问题讨论】:

    标签: java templating multilinestring


    【解决方案1】:

    您可以为此使用java.text.MessageFormat

    String[] args = {"Princess", "Luna", "Stackoverflow"};
    String text = MessageFormat.format("Bla bla, {1}, and {2} and {3}", args);
    

    【讨论】:

      【解决方案2】:

      使用 Java 15+:

      String title = "Princess";
      String name = "Luna";
      String community = "Stackoverflow";
      
      String text = """
              Dear %s %s!
              This is a question to %s-Community
              for simple approach how to code with Java multiline Strings?
              """.formatted(title, name, community);
      

      【讨论】:

        【解决方案3】:

        您可以用几行代码创建自己的小型且简单的模板引擎:

        public static void main(String[] args) throws IOException {
        
            String title = "Princes";
            String name  = "Luna";
            String community = "Stackoverflow";
        
            InputStream stream = DemoMailCreater.class.getResourceAsStream("demo.mail");
        
        
            byte[] buffer = new byte[stream.available()];
            stream.read(buffer);
        
            String text = new String(buffer);
        
            text = text.replaceAll("§TITLE§", title);
            text = text.replaceAll("§NAME§", name);
            text = text.replaceAll("§COMMUNITY§", community);
        
            System.out.println(text);
        
        }
        

        和小文本文件,例如在同一个文件夹(包)demo.mail:

        Dear §TITLE§ §NAME§!
        This is a question to §COMMUNITY§-Community
        for simple approach how to code with Java multiline Strings? 
        Like this one.
        But it must be simple approach without using of Template-Engine-Frameworks!
        
        Thx for ... 
        

        【讨论】:

        • 这会起作用,但效率会很低,尤其是随着标签数量的增加。最好使用单个 StringBuilder 并在查找令牌时扫描字符串并在找到它们时替换它们。
        【解决方案4】:

        Java 没有对模板的内置支持。您的选择是:

        • 使用现有的模板框架/引擎,
        • 构建您自己的模板框架/引擎(或类似的),或者
        • 写很多“字符串攻击”代码......就像你的问题一样。

        您可以使用String.format(...)MessageFormat 等更简洁地编写上述代码,但它们不会让您走得太远……除非您的模板非常简单。


        相比之下,一些语言内置了对字符串插值、“here”文档或可适应模板的简洁结构构建语法的支持。

        【讨论】:

          【解决方案5】:

          您可以使用Java Resources 来实现它HERE
          或者,您可以使用不同的方法(例如 HERE

          )保留当前使用的方法

          【讨论】:

            【解决方案6】:

            一种基本的方法是使用String.format(...)

            例子:

            String title = "Princess";
            String name  = "Celestia";
            String community = "Stackoverflow";
            
            String text = String.format(
                "Dear %s %s!%n" +  
                "This is a question to %s-Community%n" + 
                "for simple approach how to code with Java multiline Strings?%n" + 
                "Like this one.%n" + 
                "But it must be simple approach without using of Template-Engine-Frameworks!%n" + 
                "%n" + 
                "Thx for ...", title, name, community);
            

            More info

            【讨论】:

              【解决方案7】:

              你可以使用String#format()

              String title = "Princess";
              String name  = "Luna";
              String community = "Stackoverflow";
              String text = String.format("Dear %s %s!\n" +  
                          "This is a question to %s-Community\n" + 
                          "for simple approach how to code with Java multiline Strings?\n" + 
                          "Like this one.\n" + 
                          "But it must be simple approach without using of Template-Engine-Frameworks!\n" + 
                          "\n" +
                          "Thx for ...", title, name, community); 
              

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2010-10-18
                • 2011-01-30
                相关资源
                最近更新 更多