4

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

Rajendra arora
  • 2,186
  • 1
  • 16
  • 23
Ror Schach
  • 72
  • 6
  • 1
    Java String is immutable. `replaceAll()` returns a new String. – Abhyudaya Sharma Dec 31 '18 at 06:52
  • @AxelH not exactly, this question is more about chaining the two replacements, so as to perform both of them. – Naman Dec 31 '18 at 06:57
  • 2
    I agree @nullpointer but the answer show the correct usage of the feature. Sadly, none mentioned the immutability of `String` ... – AxelH Dec 31 '18 at 07:02
  • 2
    @AxelH While I guess it's nice to know that Strings are immutable, I'd rather say it's not the main part to be learnt here. I did rather focus on the fact that the replaceAll method returns a new String (which is due to the immutability, that's correct) which he has to work with. Nice to know the background though. – maio290 Dec 31 '18 at 07:22
  • Well @maio290, I feel that without understanding the immutable part of `String`, the result of chaining methods won't be as expected. So, to me, this is the most important part. I tried to quickly explain that in my answer. – AxelH Dec 31 '18 at 08:05

5 Answers5

5

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'
Naman
  • 27,789
  • 26
  • 218
  • 353
4

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

maio290
  • 6,440
  • 1
  • 21
  • 38
4

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.

AxelH
  • 14,325
  • 2
  • 25
  • 55
2

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"});
m fauzan abdi
  • 436
  • 5
  • 12
  • Two reasons why I don't like this answer: 1) This does not tell the OP why his solution didn't work. 2) Importing a library shouldn't be always the solution: That's like "hey, my car doesn't drive any more" - "oh, just use this new car here". – maio290 Dec 31 '18 at 07:20
  • If your project already used that library and if it offers lot of efficiency and simplicity, I think should just be fine. – m fauzan abdi Dec 31 '18 at 08:07
0
str = str.replaceAll("w", "61") //Output = 61e61
str = str.replaceAll("e", "31") //Output = 613161
Cyrus Leung
  • 106
  • 5