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>
System.out.println("axe pickaxe".replaceAll("\\baxe\\b", "sword"));
You need to use replaceAll instead of replace - because it can work with regular expressions. Then use the meta character \b which is for word boundary. In order to use it you need to escape the \ as double \ so the reges become \\baxe\\b