2

I'm often run in to the following situation: I have long multiline strings where properties must be injected - e.g. something like templating. But I don't want to inlcude a complete templating engine (like velocity or freemarker) in my projects.

How can this be done in a simple way:

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 ..."; 
4

7 回答 7

5

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

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 ... 
于 2014-01-20T11:10:20.607 回答
2

一种基本的方法是使用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);

更多信息

于 2014-01-20T11:04:46.257 回答
1

您可以在此处Java Resources使用以实现它, 或者您可以使用不同的方法保持当前使用的方法,例如此处

于 2014-01-20T11:05:07.153 回答
0

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

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

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


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

于 2014-01-20T11:04:47.350 回答
0

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

String[] args = {"Princess", "Luna", "Stackoverflow"};
String text = MessageFormat.format("Bla bla, {1}, and {2} and {3}", args);
于 2014-01-20T11:04:56.907 回答
0

您可以使用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); 
于 2014-01-20T11:03:19.533 回答
0

使用 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);
于 2021-11-10T08:33:17.253 回答