如何在 Apache FreeMarker 中格式化文本?
我想添加一个新的格式化程序来格式化值(取自 json)。例如,文本 ANIMAL
将被格式化为 Animal
(理想情况下,如果单词 animal
是小写的,它也可以将其格式化为动物
).
我检查了可用的字符串内置函数,但找不到这个特定的函数。 https://freemarker.apache.org/docs/ref_builtins_string.html
最佳答案
您可以创建一个方法来格式化您想要的单词。在示例中,我的方法 format
输入一个 String,无论它的结构如何,它都会返回第一个大写字母和其他小写字母的字符串。你可以看到我输入了“ANIMAL”这个词,它会返回“Animal”。单词是否为“aNiMaL”并不重要,它总是返回相同的结果。
public class Main {
public static void main(String[] args) {
String str = "ANIMAL";
System.out.println(format(str)); // result: Animal
}
public static String format(String input) {
return input.substring(0, 1).toUpperCase() + input.substring(1).toLowerCase();
}
}
在 FreeMarker 中使用: 修改要格式化字符串的属性中的 getter。
public class entity {
private String a;
public String getA() {
return format(this.a);
}
}
关于java - Apache FreeMarker 中的文本格式(首字母大写,其余字母小写),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74996318/