gpt4 book ai didi

java - 如何动态替换特定值的子字符串?

转载 作者:行者123 更新时间:2023-12-01 05:10:06 28 4
gpt4 key购买 nike

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

bloodpressure.properties :
(key,value) : (bloodpressure,your blood pressure was B6, this is high)

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

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

String bpstr = bp.getProperty(bloodpressure);

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

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

最佳答案

MessageFormat.format这样做:

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

基本上,您定义占位符并将其替换为您的值。这对于国际化特别有帮助,因为占位符的出现顺序可能会在不同语言中发生变化。

关于java - 如何动态替换特定值的子字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12138315/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com