原本打算用字符串截取来做这种效果,但是最近看了一些关于正则的知识,所以就用正则来试试水,比较简单就直接上代码吧
//手机号保留前3后4 中间4个*号显示
String phone = "15645621235";
System.out.println(phone.replaceAll("(\\d{3})\\d{4}(\\d{4})", "$1****$2"));
//手机号保留前3后1 中间7个*号显示
String phone1 = "17805631269";
System.out.println(phone1.replaceAll("(\\d{3})\\d{7}(\\d{1})", "$1*******$2"));
String idCard = "430529199607166698";
System.out.println(idCard.replaceAll("(\\d{4})\\d{10}(\\d{4})", "$1****$2"));