Java - String replace exact word

前端 未结 6 1197
难免孤独
难免孤独 2020-12-14 07:26
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

6条回答
  •  情歌与酒
    2020-12-14 08:04

    You can use \b to define the word boundary, so \baxe\b in this case.

    x = x.replaceAll("\\baxe\\b");
    

提交回复
热议问题