我經常遇到以下情況:我有很長的多行字符串,必須注入屬性 - 例如,就像模板一樣。但我不想在我的項目中使用完整的模板引擎(如velocity或freemarker)。在java代碼中模板化多行字符串的簡單方法
這怎麼能以簡單的方式來完成:
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 ...";
這將工作,但它將是非常低效,但特別是隨着標籤數量的增長。使用單個StringBuilder會更好,並在查找令牌時一次掃描字符串,並在找到它們時替換它們。 –