The following code will set str to "testss"
String str = "test".replaceAll("(.*)$","$1s");
Where as the following code will set it to "tests"
String str = "test".replaceFirst("(.*)$","$1s");
I would have expected both operations to produce the same result. Can someone explain why replaceAll adds an extra s to the end of the string?
This is because "(.*)$" captures two strings from "test", "test" and the empty string (""). So replaceAll will add two "s".
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With