0

我有一个字符串,其中包含像 {0}、{1} ... {n} 这样的表达式。我有长度为 n 的字符串列表。{0} 替换为 param[0],{1} 替换为 param[1],{n} 替换为 param[n]。我怎样才能做到这一点?

List<String> params = new ArrayList<String>;
param[0] = "dortmund";
param[1] = "5555";
String message = "customers address is  {0}, phone number is {1}, please check them.";
4

2 回答 2

6

您可以使用MessageFormat

MessageFormat messageFormat = new MessageFormat("customers address is  {0}, phone number is {1}, please check them.");
Object[] args = {"dortmund", "5555"};
String message = messageFormat.format(args);
于 2013-05-22T14:10:51.170 回答
-1
for (int i=0; i < params.size(); ++i) {
    message = message.replace("{"+i+"}", params[i]);
}
于 2013-05-22T14:11:58.067 回答