Domanda

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 ..."; 
È stato utile?

Soluzione

You can create your own small & simply template engine with few lines of code:

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);

}

and small text file e.g. in the same folder (package) 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 ... 

Altri suggerimenti

One basic way of doing it would be to use String.format(...)

Example:

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

You can use Java Resources in order to achieve it HERE
Or you can keep the current method you're using with different approach like HERE

With 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);

You can use 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); 

Java has no built-in support for templating. Your choices are:

  • use an existing templating framework / engine,
  • build your own templating framework / engine (or similar), or
  • write a lot of "string bashing" code ... like in your question.

You may be able to write the above code a bit more concisely using String.format(...), MessageFormat and similar, but they don't get you very far ... unless your templating is very simple.


By contrast, some languages have built-in support for string interpolation, "here" documents, or a concise structure building syntax that can be adapted to templating.

You can use java.text.MessageFormat for this:

String[] args = {"Princess", "Luna", "Stackoverflow"};
String text = MessageFormat.format("Bla bla, {1}, and {2} and {3}", args);
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top