Use a regex with word boundaries \b
:
String s = "axe pickaxe";
System.out.println(s.replaceAll("\\baxe\\b", "sword"));
The backslash from the boundary symbol must be escaped, hence the double-backslashes.
Related Contents:
- How to nicely format floating numbers to string without unnecessary decimal 0’s
- How to convert a char array back to a string?
- Java how to replace 2 or more spaces with single space in string and delete leading and trailing spaces
- How to convert a String to CharSequence?
- Why is there no String.Empty in Java? [closed]
- Sprintf equivalent in Java
- Java: how to initialize String[]?
- Why is String.chars() a stream of ints in Java 8?
- Integer.valueOf() vs. Integer.parseInt() [duplicate]
- Trim a string based on the string length
- Java String to SHA1
- Reuse a parameter in String.format?
- How to check if a string contains only digits in Java
- Best way to convert list to comma separated string in java [duplicate]
- Sort a single String in Java
- How to convert current date into string in java?
- Java Replacing multiple different substring in a string at once (or in the most efficient way)
- Access string.xml Resource File from Java Android Code
- Exact difference between CharSequence and String in java [duplicate]
- How to replace special characters in a string?
- Regex expressions in Java, \\s vs. \\s+
- Convert JsonObject to String
- Parse String to Date with Different Format in Java
- How to unescape a Java string literal in Java?
- Java replace all square brackets in a string
- Redirect console output to string in Java
- what is Ljava.lang.String;@
- Converting to upper and lower case in Java
- Remove all non-“word characters” from a String in Java, leaving accented characters?
- How to prevent java.lang.String.split() from creating a leading empty string?
- Difference between compact strings and compressed strings in Java 9
- Creating a UUID from a string with no dashes
- Difference between “char” and “String” in Java
- How to split a string between letters and digits (or between digits and letters)?
- Trim a possible prefix of a string in Java
- Java swing: Multiline labels? [duplicate]
- XOR operation with two strings in java
- Problems with newline in Graphics2D.drawString
- Make String.format(“%s”, arg) display null-valued arguments differently from “null”
- Insert string in beginning of another string
- Convert Java String to sql.Timestamp
- How to trim white space from all elements in array?
- String comparison – Android [duplicate]
- String vs char[]
- Java equivalent of Python’s format()
- Remove accents from String
- String parsing in Java with delimiter tab “\t” using split
- Comparing a string with the empty string (Java)
- Verify if String is hexadecimal
- how to check if string contains ‘+’ character [duplicate]