Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java map replace one-ot-one

For example, I have a String "PARAMS @ FOO @ BAR @" and String array {"one", "two", "three"}.

How would you map the array values one-to-one to the string (replace the markers), so that in the end I would get: "PARAMS one, FOO two, BAR three".

Thank you

like image 796
Dmitri Avatar asked Jul 13 '26 23:07

Dmitri


1 Answers

You could just do

String str =  "PARAMS @ FOO @ BAR @";
String[] arr = {"one", "two", "three"};

for (String s : arr)
    str = str.replaceFirst("@", s);

After this, str would hold "PARAMS one FOO two BAR three". Of course, to include the commas you could just replace with s + ",".

like image 113
arshajii Avatar answered Jul 15 '26 18:07

arshajii