sidzan sidzan - 1 year ago 67
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.

Answer Source

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.