1

我的属性文件中有值,例如,

bloodpressure.properties :
(key,value) : (血压,你的血压是B6,这很高)

在我的 java 类中,我正在从属性文件中读取值,但我想用计算值替换值,例如,

String B6 = "120";
Properties bp = new Properties();
bp.load(new FileInputStream("filename"));

String bpstr = bp.getProperty(bloodpressure);

现在,我想用上面的 B6 值(120)替换 B6 值。
我怎样才能动态地做到这一点?,我有很多这样的字符串。

我只想遍历属性文件,这些值应该替换为计算值。

4

3 回答 3

0

使用 String.replace()

bpstr.replace("B6",B6);

更新

或者如果您有多个 B6 实例,您可以使用 replaceAll

bpstr.replaceAll("B6",B6);
于 2012-08-27T08:12:00.270 回答
0

MessageFormat.format这样做:

String msg = "Your blood pressure is {0}";
System.out.println(MessageFormat.format(msg, 120));

基本上,您定义占位符并用您的值替换它们。这对于国际化特别有用,其中占位符的出现顺序可能会因不同的语言而改变。

于 2012-08-27T08:14:02.297 回答
0
      MessageFormat.format does that:

      String msg = "Your blood pressure is {0}";
      System.out.println(MessageFormat.format(msg, 120));

这将做你想做的事。这里,{0} 被 120 替换。

于 2012-08-27T08:23:00.753 回答