[Solved] Renaming a variable in a string format


You need to isolate your variable. How about a reg-ex?

return source.replaceAll("(\\W)(" + var2Rename + ")(\\W)", "$1" + newName + "$3");

Explanation. The \\W will check for non-letter characters, eg the boundary of a variable expression. We want a boundary on both sides of the variable, and then to replace we need to make sure the matched boundary characters are included hence the “$1”, and “$3”.

solved Renaming a variable in a string format