How to get the new value of new replaced string?

Viewed 236

Im still new at java. Is there a way to get the new string that have been replaced ?

import java.io.*;
public class Test {

    public static void main(String args[]) {
        String str = new String("wew");
        System.out.println(str.replaceAll("w", "61"));
        System.out.println(str.replaceAll("e", "31"));
    }
}

output:

61e61
w31w

Desired new output:

613161

I want to get the output string 61e61 then replaced the e to 31

5 Answers

You can chain replaceAll as:

System.out.println(str.replaceAll("w", "61").replaceAll("e", "31"));

Currently, you're returning two different strings with both your print statements.

System.out.println(str.replaceAll("w", "61")); // returns new string '61e61'
System.out.println(str.replaceAll("e", "31")); // returns new string 'w31w'

You're using it wrong. The method replaceAll of the Class String returns a String.

You have to use the return value again (which can be written in one line):

    String str = "wew".replaceAll("w", "61").replaceAll("e", "31");
    System.out.println(str);

Outputs: 613161

Let's review why your code doesn't provide what you expect.

You create an instance with "wew".

String str = new String("wew");

Then you replace "w" in "wew" with "61" and print the result "61e61"

System.out.println(str.replaceAll("w", "61")); //61e61

Now, the important part here is that the result of str.replaceAll is a new instance, so str is still "wew".

System.out.println(str); // wew

This explain why the second replacement will print "w31w"

System.out.println(str.replaceAll("e", "31")); //w31w

The reason is that String are immutable, so when you try to change the value of an immutable instance, a new instance is return. So to keep it, you need to assign that instance to a variable.

str = str.replaceAll("w", "61");

Now, the result is kept in the variable "str"

System.out.println(str); // 61e61

Now, one good thing about immutable classes is that method can usually be chained because the return value is an instance of the class itself. So you can call multiple method at one.

str = str.replaceAll("w", "61").replaceAll("e", "31");
System.out.println(str); // 613161

But if you want to print the intermediate result, you will need do it in two statement

    str = str.replaceAll("w", "61");
    System.out.println(str); // 61e61
    System.out.println(str = str.replaceAll("e", "31")); // 613161

Note the last statement, it is possible to merge both assignment and print statement. First str = .. will be evaluated then the result will be printed.

String.replaceAll() will return a new String so that your code System.out.println(str.replaceAll("w", "61")); and System.out.println(str.replaceAll("e", "31")); need to be chained, else will return wrong result,
you can use StringUtils.replaceEach() from commons-lang3:

StringUtils.replaceEach("wew", new String[]{"w", "e"}, new String[]{"61", "31"});
str = str.replaceAll("w", "61") //Output = 61e61
str = str.replaceAll("e", "31") //Output = 613161
Related