I like this one because I just want to hide 4 characters, it also dynamically decrease the hidden chars to 2 if the email address is too short:
public static String maskEmailAddress(final String email) { final String mask = "*****"; final int at = email.indexOf("@"); if (at > 2) { final int maskLen = Math.min(Math.max(at / 2, 2), 4); final int start = (at - maskLen) / 2; return email.substring(0, start) + mask.substring(0, maskLen) + email.substring(start + maskLen); } return email;}
Sample outputs:
my.email@gmail.com > my****il@gmail.cominfo@mail.com > i**o@mail.com