例如,我有一个字符串 "PARAMS @FOO @BAR @"
和字符串数组 {"one", "two", "three"}
。
如何将数组值一对一映射到字符串(替换标记),这样最后我会得到:"PARAMS one, FOO two, BAR three"
.
谢谢
最佳答案
你可以这样做
String str = "PARAMS @ FOO @ BAR @";
String[] arr = {"one", "two", "three"};
for (String s : arr)
str = str.replaceFirst("@", s);
在此之后,str
将包含 "PARAMS one FOO two BAR three"
。当然,要包含逗号,您可以将其替换为 s + ","
。
关于Java map 一对一替换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13112989/