String x = \"axe pickaxe\"; x = x.replace(\"axe\", \"sword\"); System.out.print(x);
By this code, I am trying to replace the exact word axe>
axe>
You can use \b to define the word boundary, so \baxe\b in this case.
\b
\baxe\b
x = x.replaceAll("\\baxe\\b");