Search code examples
replacejasper-reports

jasper replace String code


I have a string variable wich contain only one letter like "D" or "A" etc ...
I want to replace this letter by an explicit text but when I do (in this case "$F{Action}" contain "D"):

$F{Action}.replace('D','Apple').replace('A','text')

my result is "textpple" Because Apple beggin with an "A" and my second replace is on the letter "A"

How can I do to only replace the letter by the firt replace statment and not do the others replace statement?


Solution

  • There is no need of scriptlet for this small work. You can try using the below expression

    $F{Action}.contains( "D" ) ? "Apple" : $F{Action}.contains( "A" ) ? "text" : ""
    

    Hope this is your requirement.