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 aren't doing anything with the return value of replace
. You'll need to assign the result of the method, which is the new String
:
sentence = sentence.replace("and", " ");
A String
is immutable in java. Methods like replace
return a new String
.
Your contains
test is unnecessary: replace
will just no-op if there aren't instances of the text to replace.