How do I replace a substring with a masking character in Visualforce? For eg. I have a list of Contacts under an Account to be displayed on the Visualforce page - email field to be masked.

[email protected] to be displayed as [email protected]

I have tried left() and right() to get us and [email protected] respectively. Finding it challenging to replace all intermediate characters to x.


1 Answer 1


Would something like this suffice? I’ll leave it up to you to define what happens if the part before the @ is 2 or less characters.

String valueToMask = original.substring(2, original.indexOf('@'));
String mask = 'x'.repeat(valueToMask.length());
String masked = original.replaceFirst(valueToMask, mask);

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .