public class NewClass {
public static void main(String[] args) {
String piece = "10*2";
String ans = "20";
String problm = "10*2*9";
System.out.println(piece);
System.out.println(ans);
problm.replaceAll(piece, ans);
System.out.println(problm);
}
}
This is my code, and when I print problm
it prints: 10*2*9
I want it to print 20*9
, please tell me what I'm doing wrong
First of all, use replace
instead of replaceAll
. replaceAll
uses regular expressions, which you probably don't want.
Secondly, Strings in Java are immutable, meaning that their value can never be changed. You need to assign the value of problm.replaceAll(piece, ans);
back to problm
:
problm = problm.replace(piece, ans);