I have a sentence that is passed in as a string and I am doing a replace on the word \"and\" and I want to replace it with \" \". And it\'s not replacing the word \"and\" w
You should re-assign the result of the replacement, like this:
sentence = sentence.replace("and", " ");
Be aware that the String
class is immutable, meaning that all of its methods return a new string and never modify the original string in-place, so the result of invoking a method in an instance of String
must be assigned to a variable or used immediately for the change to take effect.