Java 您使用什么类来制作字符串模板?
您使用什么类使字符串占位符工作Java 您使用什么类来制作字符串模板?,java,string,Java,String,您使用什么类使字符串占位符工作 String template = "You have %1 tickets for %d", Brr object = new Brr(template, {new Integer(1), new Date()}); object.print(); 怎么样 它的缩写包括和。String。格式是最简单的: String s = String.format("%s %s", "Hello", "World!"); 您可以使用数量可变的参数来调用它,如我上面
String template = "You have %1 tickets for %d",
Brr object = new Brr(template, {new Integer(1), new Date()});
object.print();
怎么样
它的缩写包括和。
String。格式是最简单的:
String s = String.format("%s %s", "Hello", "World!");
您可以使用数量可变的参数来调用它,如我上面所示,或者向它传递一个Object
数组,它将使用该数组 以下方法应该有效:
import java.util.*;
class Brr {
String template;
Object[] args;
public Brr(String template, Object... args) {
this.template = template;
this.args = args;
}
public void print() {
System.out.println(String.format(template, args));
}
}
public class Test {
public static void main(String... args) {
String template = "You have %d tickets for %tF";
Brr object = new Brr(template, new Integer(1), new Date());
object.print();
}
}
输出:
You have 1 tickets for 2010-06-01
如果需要转换的完整参考,请查看。允许我使用顺序参数,从而轻松启用i18n
private final Map<String, String> localizedMessages = new HashMap<String, String>();
private void init() {
this.localizedMessages.put("de_DE", "{2} Suchtreffer, zeige Ergebnisse ${0} bis ${1}");
this.localizedMessages.put("en_US", "Showing results {0} through {1} of a total {2");
}
public String getLocalizedMessage(final String locale,
final Integer startOffset, final Integer endOffset,
final Integer totalResults) {
return MessageFormat.format(this.localizedMessages.get(locale),
startOffset, endOffset, totalResults);
}
private final Map localizedMessages=new HashMap();
私有void init(){
this.localizedMessages.put(“de_de”,“2}Suchtreffer,zeige Ergebnisse${0}bis${1}”);
this.localizedMessages.put(“en_US”,“显示总共{2}中{0}到{1}的结果”);
}
公共字符串getLocalizedMessage(最终字符串区域设置,
最终整数startOffset,最终整数endOffset,
最终整数(结果){
return MessageFormat.format(this.localizedMessages.get(locale)),
startOffset、endOffset、totalResults);
}
您有两个选择:
-
- 用于
printf
样式格式字符串的解释器。此类支持布局对齐和对齐、数字、字符串和日期/时间数据的通用格式以及特定于语言环境的输出
- 。
MessageFormat
提供了一种以与语言无关的方式生成串联消息的方法。使用此方法可构造为最终用户显示的消息
在这两种格式中,MessageFormat
的功能要强大得多。下面是一个用于以不同方式处理0
、1
和>1
案例的示例:
import java.text.MessageFormat;
import java.util.Date;
//...
String p = "You have {0,choice,0#none|1#one ticket|1<{0,number,integer} tickets} for {1,date,full}.";
for (int i = 0; i < 4; i++) {
System.out.println(MessageFormat.format(p, i, new Date()));
}
文档中还有更多的示例。如果您需要更强大的字符串模板,Apache Velocity库非常有用Rythm一个java模板引擎,现在发布了一个名为的新功能,允许您执行以下操作:
String result = Rythm.render("You have @num tickets for @date", 1, new Date());
上述案例显示,您可以按位置将参数传递给模板。Rythm还允许您按名称传递参数:
Map<String, Object> args = new HashMap<String, Object>();
args.put("num", 1);
args.put("date", new Date());
String result = Rythm.render("You have @num tickets for @date", args);
Map args=newhashmap();
参数put(“num”,1);
args.put(“日期”,新日期());
String result=Rythm.render(“您有@date的@num票证”,args);
链接:
- 检查
- 阅读
- 下载或
Map<String, Object> args = new HashMap<String, Object>();
args.put("num", 1);
args.put("date", new Date());
String result = Rythm.render("You have @num tickets for @date", args);