加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

java-如何从如下所示的列表值进行字符串操作

发布时间:2020-12-14 19:25:17 所属栏目:Java 来源:网络整理
导读:我想动态地用列表值替换字符串,而无需硬编码. 就像“ list.get(0)” 在第一次迭代中,==> str = str.replace(“ {Name}”,一个); 在第二迭代中,==> str = str.replace(“ {subInterfaceId}”,Two); 提前致谢. String str = "Iamstillquite/{Name}/newtoJava/p

我想动态地用列表值替换字符串,而无需硬编码.
就像“ list.get(0)”

在第一次迭代中,==> str = str.replace(“ {Name}”,’一个’);

在第二迭代中,==> str = str.replace(“ {subInterfaceId}”,’Two’);

提前致谢.

String str = "Iamstillquite/{Name}/newtoJava/programm/ingandIam/{subInterfaceId}/tryingtoupdate/anexisting";
List<String> list = new ArrayList<>();
list.add("One");
list.add("Two");    

for (String s : list) {
    str = str.replace("{Name}",s);
}   

预期产量:

String finalstr = "Iamstillquite/One/newtoJava/programm/ingandIam/Two/tryingtoupdate/anexisting";
最佳答案
您需要一个地图.它将键({名称})映射到值(一个).

Map<String,String> map = new HashMap<>();
map.put("{Name}","One");
map.put("{subInterfaceId}","Two");

for (String key : map.keySet()) {
    str = str.replace(key,map.get(key));
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读