我有一个字符串,其中包含类似 {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.";
最佳答案
您可以使用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);
关于java 通过正则表达式更改包含 {n} 的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16693979/