I want to mask the email in this format xxx####xx##@x####.x##
.
x
-> to display the character , #
-> to hide the character.
For example:
- input:
testemail@gmail.com
, - output:
tes##ma##@g####.c##
This is the code I have written
private static String maskEmailId(String email, String format) {
int start = format.indexOf("#");
int end = email.indexOf("@");
String result = maskPattern(start, end, email);
start = email.indexOf('@')+ (format.indexOf("#", format.indexOf("@")) - format.indexOf("@") - 1);
end = email.indexOf('.');
result = maskPattern(start + 1, end, result);
start = email.indexOf(".") + (format.indexOf("#", format.indexOf(".")) - format.indexOf(".") - 1);
end = email.length();
result = maskPattern(start + 1, end, result);
return result;
}
private static String maskPattern(int start, int end, String email) {
StringBuilder sb = new StringBuilder(email);
for (int i = start; i < end; i++) {
sb.setCharAt(i, '#');
}
return sb.toString();
}
I am bit confused with the logic if I want to display and hide in between after hiding first time.
Can anyone help on this?