How to replace a character with a unicode character?

How to replace the symbol - in the text that comes from the database in the jasper field, with the image? The purpose is the illustration below:

target

Something like: $F{KN_Zusatzinfo_DV_Einleitungstext}.replaceAll("- ", "[\\x254]") , where x254 is the ascii code for the red square.

But the above code writes the ascii code as it is in the text and does not create the image.

+2
source share
1 answer

The correct syntax for displaying the unicode character \uXXXX

For example, this expression:

 $F{listItem}.replaceAll("-", "\u2588") 

Will do this

Output

Now you like the dot to be red , so we need to apply some style, set markup="html" to textElement and replace the replacement with this

 $F{listItem}.replaceAll("-", "<font color=\"red\">\u2588</font>") 

It will display:

Red dots

Note: You need to be careful with the regular expression in replaceAll , I probably ^- , therefore, starts with - (to avoid replacing others - in the text), in addition, the usual way would be to simply add a red rectangular element to each row. Also take care of font extensions if you export pdf so that your font displays correctly

+3
source

Source: https://habr.com/ru/post/977204/


All Articles