// The array of values is in the form { "var1", "val1", "var2", "val2",.. }
public static String replaceMethod(String template, String... values) {
String p = template;
for (int i = 0; i < values.length; i += 2) {
p = p.replace(values[i], values[i+1]);
}
return populatedTemplate;
}
This method replaces a set of sub-strings with respectives values in a string. Ex:- string => abcd sub-string array of values ["a","b","c","d"] It means replace 'a' with 'b' and 'b' with 'c'.
So, now comes the issue.
1.My above implementation replaces 'a' with 'b' in first loop and then in second loop, it replaces 'b' with 'c' , making final string as "cccd". But I want the out as "bccd". Replacement in series uses the previous replaced string to work further. Is there a way to parallel replace all the sub-strings in a more efficient way.
2.How can I implement above method using Map, instead of array (second argument of above method)? Is Map better, or above implementation is fine ?