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

5
  • 1
    Java String is immutable. replaceAll() returns a new String. Commented Dec 31, 2018 at 6:52
  • @AxelH not exactly, this question is more about chaining the two replacements, so as to perform both of them. Commented Dec 31, 2018 at 6:57
  • 2
    I agree @nullpointer but the answer show the correct usage of the feature. Sadly, none mentioned the immutability of String ... Commented Dec 31, 2018 at 7: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. Commented Dec 31, 2018 at 7: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. Commented Dec 31, 2018 at 8:05

5 Answers 5

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' 
Sign up to request clarification or add additional context in comments.

Comments

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

1 Comment

thank you so much sir!. I will try my best learn more on JAVA
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.

Comments

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"}); 

2 Comments

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".
If your project already used that library and if it offers lot of efficiency and simplicity, I think should just be fine.
0
str = str.replaceAll("w", "61") //Output = 61e61 str = str.replaceAll("e", "31") //Output = 613161 

2 Comments

Your answer contains the solution, but please add a modicum of explanation in addition to your code to show what the problem was and how you solve it.
thank you so much sir!. i really appreciate it sir

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.