I have seen Mail services displays the email id's as e*****[email protected]
, mostly in their recovery page.
So i am trying to replace the [email protected]
as e*****[email protected]
.
Is it possible to achieve it using String#replace(String)
alone ? or should i use some REGEX to achieve it .
Thanks for your valuable suggestions in adavance
Search regex:
\b(\w)\S*?(\S)(?=@)(\S+)\b
Replacement Pattern:
$1****$2$3****$4
Code:
String email = "[email protected]";
String repl = email.replaceFirst("\\b(\\w)\\S*?(\\S@)(\\S)\\S*(\\S\\.\\S*)\\b",
"$1****$2$3****$4");
//=> a****e@g****l.com
You can try without regex too
String email = "[email protected]";
int start = 1;
int end = email.indexOf("@") - 1;
StringBuilder sb = new StringBuilder(email);
StringBuilder sb1=new StringBuilder();
for(int i=start;i<end;i++){
sb1.append("*");
}
sb.replace(start, end, sb1.toString());
System.out.println(sb.toString());
Out put:
e*****[email protected]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With