You have
user.nick@domain.com
and result should be:
******@domain.com
Currently I’m doing it this way:
public static String removeUserFromEmail(String email) {
StringBuffer sbEmail = new StringBuffer(email);
int start = sbEmail.indexOf("@");
sbEmail.delete(0, start);
return "******" + sbEmail.toString();
}
Is there something simpler or more elegant?
Nothing wrong with that solution, although I have two suggestions:
1) Use StringBuilder instead of StringBuffer unless you need to synchronize access between multiple threads. There is a performance penalty associated with StringBuffer that for this application is likely unnecessary.
2) One of the benefits of StringBuilder/Buffer is avoiding excessive string concatenations.
Your return line converts the Buffer to a string, and then concatenates. I would probably do this instead:
FYI – my solution is really just some thoughts on your current use of StringBuffer (which are hopefully helpful). I would recommend Konstantin’s solution for this simple string exercise. Simple, readable, and it gives you the opportunity to handle the error condition.