This is my code.
String x = "1+√10";
x = x.replace(".*", "x");
System.out.println(x);
This should return "x" but istead it is returning "1+√10". Why isn't this working?
asked Jan 26, 2014 at 13:59
usama8800
8753 gold badges10 silver badges20 bronze badges
-
This should return "x" - No, no ".*" found in the String.Maroun– Maroun2014年01月26日 13:59:50 +00:00Commented Jan 26, 2014 at 13:59
1 Answer 1
String#replace doesn't support regex, use String#replaceAll:
x = x.replaceAll(".+", "x");
answered Jan 26, 2014 at 13:59
anubhava
791k67 gold badges604 silver badges671 bronze badges
Sign up to request clarification or add additional context in comments.
1 Comment
anubhava
See updated answer, you need
.+ instead of .* since .* matches twice once for empty string and once for full string.lang-java