1

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?

1
  • This should return "x" - No, no ".*" found in the String. Commented Jan 26, 2014 at 13:59

1 Answer 1

7

String#replace doesn't support regex, use String#replaceAll:

x = x.replaceAll(".+", "x");
Sign up to request clarification or add additional context in comments.

1 Comment

See updated answer, you need .+ instead of .* since .* matches twice once for empty string and once for full string.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.