Wednesday, March 4, 2015

keep first 4 characters of email address and replace the rest with * in java code

String s = "msdgsalasdasdehi@gmail.com";
int atIndex = s.indexOf("@");
String localPart = String.format("%-" + atIndex + "s", s.substring(0, 4)).replaceAll("\\s", "*");
String domainPart = s.substring(atIndex);
System.out.println(localPart + domainPart);

No comments:

Post a Comment