android 如何在java中显示部分电子邮件地址

osh3o9ms  于 2023-08-01  发布在  Android
关注(0)|答案(1)|浏览(84)

我想知道如何使用java显示电子邮件地址的一部分。举例来说:firstname@gmail.com到fi***me@gmail.com使用java编程
举例来说:firstname@gmail.com到fi
***me@gmail.com使用java编程

gmxoilav

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;
        }
    }
}

字符串

相关问题