Possible Duplicate:
Hints for java.lang.String.replace problem?

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" with white space. Below is an example of my logic. And when I debug this the logic does fall into the sentence.replace.

    String sentence = "Define, Measure, Analyze, Design and Verify"
    if (sentence.contains("and")){
        sentence.replace("and", " ");
    }

Is there something I am missing here.

share|improve this question
2  
Strings are immutable. – Matt Ball Oct 4 '12 at 19:46

marked as duplicate by Matt Ball, pb2q, tim_yates, David Basarab, dystroy Oct 5 '12 at 17:22

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

4 Answers

up vote 2 down vote accepted

No need for contains() as its and unnecessary call over here

public class Test{

    public static void main(String[] args) {

        String sentence = "Define, Measure, Analyze, Design and Verify";

        String str = sentence.replace("and", "");
        System.out.println(str);

    }
}
share|improve this answer
Thank you Kumar. – Mark Basler Oct 4 '12 at 22:19
2  
The text of this answer implies that it's the contains() call which was causing a problem - it wasn't. It was an unnecessary call, but it didn't actually cause any issues. The problem was due to ignoring the return value of replace, which isn't explained at all in the answer. – Jon Skeet Oct 4 '12 at 22:50
@Mr.Jon Skeet i am really sorry, if the text in the answer implied the other way around...but when i said its not required, i meant it was unnecessary.... fine i will include that in the answer making it more obvious..... – Kumar Vivek Mitra Oct 5 '12 at 6:08

And when I debug this the logic does fall into the sentence.replace.

Yes, and then you discard the return value.

Strings in Java are immutable - when you call replace, it doesn't change the contents of the existing string - it returns a new string with the modifications. So you want:

sentence = sentence.replace("and", " ");

This applies to all the methods in String (substring, toLowerCase etc). None of them change the contents of the string.

Note that you don't really need to do this in a condition - after all, if the sentence doesn't contain "and", it does no harm to perform the replacement:

String sentence = "Define, Measure, Analyze, Design and Verify";
sentence = sentence.replace("and", " ");
share|improve this answer
I've got to call you out on answering a dup instead of voting to close. This problem has been asked & answered so many times before - what's the deal, Jon? – Matt Ball Oct 4 '12 at 19:49
@MattBall While agree the question has being asked repeatedly, I think this is a better answer, IHMO – MadProgrammer Oct 4 '12 at 19:58
1  
@MattBall: As so often happens, I find it quicker to give a good, complete answer than to find a duplicate which also has a good answer. I believe (somewhat egotistically, admittedly) that my answer here is better than the accepted one in the duplicate you found, which doesn't even really make technical sense. ("You need to make your string actually equal the changes you make to the string" - what?) Additionally, I wanted to point out the aspect about not needing to check for containment first. – Jon Skeet Oct 4 '12 at 20:02

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.

share|improve this answer
My contain logic is needed because there are cases where I will not want to replace this. The logic isn't exactly like this, I was just using this as a example. – Mark Basler Oct 4 '12 at 19:58
@MarkBasler: Your contain logic isn't needed for the sample you gave, so you shouldn't have included it. Good questions should contain only what they need to show the problem - by including code which was pointless in the situation you provided, you've added a distraction to the question. – Jon Skeet Oct 4 '12 at 20:04
I was concerned with the contain I was concerned with the replace. – Mark Basler Oct 4 '12 at 22:19

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.

share|improve this answer
Strings should be immutable, but there are bugs with the String lib, that allow you to modify the original string. – Mark Basler Oct 4 '12 at 20:04
1  
@MarkBasler: Um, what bugs are you talking about, exactly? And if you're aware that strings are immutable, it's unclear why you expected your code to work at all... – Jon Skeet Oct 4 '12 at 22:49

Not the answer you're looking for? Browse other questions tagged or ask your own question.