javastringjasper-reports

How to replace first few characters in java?


I have a situation where i have to replace first 4 characters of a string to 'x'.

The original data is '12345678'. I want to replace to 'xxxx5678'.

I am actually using Jasper Reports and this is my field:

$F{obj.contact_id.hisher_member_id}

Any suggestion will be highly appreciated.


Solution

  • Community wiki from Yassin Hajaj suggestion and Bethany Louise comment

    In Java, using substring, a solution is:

    String output = "xxxx" + input.substring(4); 
    

    That in Jasper Reports <textFieldExpression/> is

    <textFieldExpression><![CDATA["xxxx" + $F{obj.contact_id.hisher_member_id}.substring(4)]]></textFieldExpression>
    

    Note: the field $F{obj.contact_id.hisher_member_id} class should be java.lang.String.