java将邮件号码字符左边两个字符换为
要将邮件号码中@字符左边两个字符换为*,可以使用字符串的substring()方法和replace()方法来实现。具体代码如下:
public class Main {
public static void main(String[] args) {
String email = "abc123@example.com";
int atIndex = email.indexOf("@"); // 获取@字符的索引
String replacedEmail = email.substring(0, atIndex-2) + "**" + email.substring(atIndex); // 使用substring()方法获取@字符左边两个字符并替换为*
System.out.println(replacedEmail);
}
}
输出结果为:ab**123@example.com
原文地址: https://www.cveoy.top/t/topic/hMK7 著作权归作者所有。请勿转载和采集!