I’ms looking for a way to replace my variables in a string by their value. Here is my string lookalike:
"cp $myfile1 $myfile2"
In fact, I looked the javadoc and it seems that I could use split() method from String class which is good but I have also seen an article on which it seems that it is possible to replace all my variables with regex and replaceAll() method. Unfortunately I didnt find any example on the last solution.
Is it possible to use replaceAll in my case (with an example)?
No, you can’t use
String.replaceAllin this case. (You could replace all$...substrings, but each replacement would depend on the actual variable being replaced.)Here’s an example that does a simultaneous replacement which depends on the substring being replaced:
Output:
(adapted from over here: Replace multiple substrings at once)