I’m trying to remove a specific word from a certain string using the function replace() or replaceAll() but these remove all the occurrences of this word even if it’s part of another word!
Example:
String content = "is not like is, but mistakes are common";
content = content.replace("is", "");
output: "not like , but mtakes are common"
desired output: "not like , but mistakes are common"
How can I substitute only whole words from a string?
What the heck,
Remember you need to use
replaceAll(...)to use regular expressions, notreplace(...)\\bgives you the word boundaries\\s*sops up any white space on either side of the word being removed (if you want to remove this too).