sidzan sidzan - 5 months ago 13
Java Question

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:


Any suggestion will be highly appreciated.


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 needs to be java.lang.String.