簡體   English   中英

在java代碼中模板化多行字符串的簡單方法

[英]Simple way templating multiline strings in java code

我經常遇到以下情況:我有很長的多行字符串,必須在其中注入屬性 - 例如模板之類的東西。 但我不想在我的項目中包含一個完整的模板引擎(如速度或自由標記)。

如何以簡單的方式完成此操作:

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

您可以使用幾行代碼來創建自己的小型模板引擎:

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 ... 

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

更多信息

您可以在此處使用Java Resources來實現它
或者,您可以使用其他方法(例如HERE)保留當前使用的方法

您可以使用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沒有內置的模板支持。 您的選擇是:

  • 使用現有的模板框架/引擎,
  • 構建自己的模板框架/引擎(或類似模板),或
  • 像在您的問題中一樣,編寫大量的“字符串打擊”代碼。

您可以使用String.format(...)MessageFormat和類似的代碼更簡潔地編寫上面的代碼,但是它們不會使您走得太遠……除非您的模板非常簡單。


相比之下,某些語言內置了對字符串插值,“此處”文檔或簡潔的結構構建語法(可適用於模板)的支持。

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

String[] args = {"Princess, "Luna", "Stackoverflow"};

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

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM