-
Notifications
You must be signed in to change notification settings - Fork 88
/
EmailMaskerOptimised.java
46 lines (37 loc) · 1.58 KB
/
EmailMaskerOptimised.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
public class EmailMaskerOptimised {
public static String maskEmail(String email) {
if (email == null || email.isEmpty()) {
return email;
}
int atIndex = email.indexOf('@');
if (atIndex <= 0) {
return email;
}
// Get the local part and domain of the email
String localPart = email.substring(0, atIndex);
String domain = email.substring(atIndex);
int length = localPart.length();
int maskLength = Math.max(length / 2, 1); // Mask at least half of the characters
int startIndex = (length - maskLength) / 2; // Start masking from the middle of the local part
StringBuilder maskedLocalPart = new StringBuilder(localPart);
for (int i = startIndex; i < startIndex + maskLength; i++) {
maskedLocalPart.setCharAt(i, '*');
}
// Return the masked email
return maskedLocalPart.append(domain).toString();
}
public static void main(String[] args) {
String email1 = "[email protected]";
String email2 = "[email protected]";
String email3 = "johndoe@";
System.out.println("Original Email: " + email1);
System.out.println("Masked Email: " + maskEmail(email1));
System.out.println();
System.out.println("Original Email: " + email2);
System.out.println("Masked Email: " + maskEmail(email2));
System.out.println();
System.out.println("Original Email: " + email3);
System.out.println("Masked Email: " + maskEmail(email3));
System.out.println();
}
}