Can you explain the output
String str = "Total Amount is AMOUNT";
String amount = "$10.00";
str = str.replaceAll("AMOUNT", amount);
System.out.println(str);
What is the output? It throws exception
Exception in thread "main" java.lang.IndexOutOfBoundsException: No group 1
By removing $ its working.Why?
String.replaceAll()
accepts a regex.
And $
in regex are used for replacing the captured groups. Like $1
represent's the content of first captured group... and so on.
In your case, since you don't use regex at all just use String.replace("AMOUNT", amount)
$
is a special char in regex.
You can escape it by using \\
String amount = "\\$10.00";
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