I get a string-template and a variable-length argument list. I need I need to insert arguments into the template and send the result.
For instance:
template: "%1s test %2s test %1s"
args: "CAT", "DOG"
Result: "CAT test DOG test CAT"
I tried to do it like this. But I got an error, because in fact, I'm trying to execute the string String.format("%1s test %2s test %1s", "value") which is really wrong.
public static void main(String[] args) {
getStringFromTemplate("%1s test %2s test %1s", "CAT", "DOG");
}
public void getStringFromTemplate(String template, String... args){
ArrayList<String> states = new ArrayList<>();
Collections.addAll(states, args);
String s;
Iterator<String> iter = states.iterator();
while(iter.hasNext()){
s = String.format("%1s test %2s test %1s", iter.next());
}
rerurn s;
}