0

I have the following Test:

public void testStringReplace()
    {    
        final String placeholder = "$ph$";
        final String template = "<test>" + placeholder + "</test>";
        final String result = "<test>Hello!</test>";

        String copyOfTemplate = template;
        copyOfTemplate.replace(placeholder, "Hello!");

        if(!copyOfTemplate.equals(result));
            fail();
    }

The test always fails, but why? How do I have to define copyOfTemplate, to be able to change it? Or am I missing some other detail here?

2
  • 1
    copyOfTemplate = copyOfTemplate.replace(placeholder, "Hello!"); Commented Oct 11, 2018 at 9:24
  • 3
    if(!copyOfTemplate.equals(result)); <-- Remove that semicolon at the end! Commented Oct 11, 2018 at 9:25

5 Answers 5

3

String is immutable so calling

copyOfTemplate.replace(placeholder, "Hello!");

without assigning it to anything effectively does nothing. It returns a new string with the replacement, which you're ignoring. Any half-decent IDE will warn you about this:

IDE warning

Also, String copyOfTemplate = template doesn't really do anything either. It's not a copy. It's just a new variable pointing to the same underlying string. There is no method to copy a string because, again, strings are immutable so a copy becomes useless.

You want

String copyOfTemplate = template.replace(placeholder, "Hello!");

I suggest reading the Oracle tutorial on strings. It seems like you've missed some of the fundamentals.

Sign up to request clarification or add additional context in comments.

Comments

0

As strings are immutable, opyOfTemplate.replace(placeholder, "Hello!"); produces a new string. So, you have to review your variable

copyOfTemplate = copyOfTemplate.replace(placeholder, "Hello!");

Comments

0

I see two main problems in your code:

  • Your use of String#replace

    copyOfTemplate.replace(placeholder, "Hello!"); returns a new string, it doesn't upadate it. You have to assign it to a new variable.

  • The if statement at the end

    if(!copyOfTemplate.equals(result)); Since you added a semicolon, the if does nothing, and you always reach the fail() method. it's as if you wrote:

    if(!copyOfTemplate.equals(result)) {
    
    }
    fail();
    

Comments

0

Commented some of the mistakes in your code below -

 final String placeholder = "$ph$";
final String template = "<test>" + placeholder + "</test>";
final String result = "<test>Hello!</test>";

String copyOfTemplate = template;
copyOfTemplate = copyOfTemplate.replace(placeholder, "Hello!"); 
// you cannot change existing object because string is immutable

if(!copyOfTemplate.equals(result)) // semi colon removed
    fail();

Comments

0

Please use below code

public static void main(String[] args) {

        final StringBuilder placeholder = new StringBuilder("$ph$");
        final StringBuilder template = new StringBuilder("<test>" + placeholder + "</test>");
        final StringBuilder result = new StringBuilder("<test>Hello!</test>");

        replaceString(template, placeholder.toString(), "Hello!");
        System.out.println(template);

    }

    public static void replaceString(StringBuilder sb, String toReplace, String replacement) {
        int index = -1;
        while ((index = sb.lastIndexOf(toReplace)) != -1) {
            sb.replace(index, index + toReplace.length(), replacement);
        }
    }

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.