Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java .replaceAll() for $ and \

I have use the replaceAll() function in my Java ireport for one of the text field. The following codes need to replace all the matche strings to $ sign or \ character . However, it is only work for replace() function only.

$P{name}.replaceAll('abc', '\$');

or

$P{name}.replaceAll('abc', '\\');
like image 313
Elice Ee Avatar asked Dec 14 '25 02:12

Elice Ee


1 Answers

Use double escape character \

String str = "abc-d-abc";
str = str.replaceAll("abc", "\\$");
System.out.println(str);

String str1 = "abc-d-abc";
str1 = str1.replaceAll("abc", "\\\\");
System.out.println(str1);

replace: It will replace all occurrence of Character/String matched in String. replace can't process Regular Expression.

replaceAll: It will replace all occurrence of Character/String matched in String. replaceAll can process Regular Expression. Its slower because it has to process regular expression

like image 50
Vicky Thakor Avatar answered Dec 15 '25 14:12

Vicky Thakor



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!