[英]Java String not replacing with hashmap values
for (Entry<String, String> entry : map.entrySet()) {
String delimiter = "**";
result = result.replace(delimiter + entry.getKey() + delimiter, entry.getValue());
}
result is my string to be replaced by hashmap values. 结果是将我的字符串替换为hashmap值。 Here string (result variable) is returning as itself not replacing any value.
在这里,字符串(结果变量)返回本身而不替换任何值。 Please any one have suggestions ?
请任何人有建议吗?
From comment 来自评论
My hashmap contains, 我的哈希图包含
HashMap<String, String> map = new HashMap<String, String>();
map.put("Rid", serviceBooking.getId().toString());
map.put("Rname", customer.getName());
map.put("Rnic", "");
Either your HashMap
is empty, or the original string doesn't contain anything corresponding to the keys in the map, or you wrote two asterisks where you meant one, or you didn't escape it/them when you needed to, or ... 您的
HashMap
为空,或者原始字符串不包含与地图中的键相对应的任何内容,或者您在意为一个的位置写了两个星号,或者在需要时未进行转义,或。 。
Impossible to improve on that without seeing the original string and the contents of the map. 无法在不查看原始字符串和地图内容的情况下进行改进。
The algorithm OK Here's a working, executable example: 算法确定这是一个可行的可执行示例:
// sample input
String input = "abcd **Rid** efgh";
// a small map
Map<String, String> map = new HashMap<String, String>();
map.put("Rid","VALUE");
// the loop that replaces the **Rid** substring
for (Map.Entry<String,String> entry:map.entrySet()){
input = input.replace("**"+entry.getKey()+"**", entry.getValue());
System.out.println(input);
}
It prints 它打印
abcd VALUE efgh
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.