我想知道如何使用java显示电子邮件地址的一部分。举例来说:firstname@gmail.com到fi***me@gmail.com使用java编程举例来说:firstname@gmail.com到fi***me@gmail.com使用java编程
gmxoilav1#
这应该给予你一个起点。
public class EmailObfuscator { public static void main(String[] args) { String email = "firstname@gmail.com"; String obfuscatedEmail = obfuscateEmail(email); System.out.println(obfuscatedEmail); } public static String obfuscateEmail(String email) { int atIndex = email.indexOf("@"); if (atIndex > 4) { String firstPart = email.substring(0, 2); String lastPart = email.substring(atIndex - 2); StringBuilder obfuscatedPart = new StringBuilder(); for (int i = 2; i < atIndex - 2; i++) { obfuscatedPart.append("*"); } return firstPart + obfuscatedPart + lastPart; } else { // Email address is too short decide what to do return email; } } }
字符串
1条答案
按热度按时间gmxoilav1#
这应该给予你一个起点。
字符串