How to get the new value of new replaced string?

  • A+
Category:Languages

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'

 


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' 

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: