目录
方式1:substring分割
方式2:正则匹配验证
方式3:StringUtils.overlay
1 先引入commons-lang3,这个基本每个项目都用到
2 数据处理
3 测试结果
String phone = "13123456789";
String phoneNumber = phone.substring(0, 3) + "****" + phone.substring(7, phone.length());
System.out.println("phone总长度:" + phone.length());
System.out.println("phone前3位:" + phone.substring(0, 3));
System.out.println("phone中4位:" + phone.substring(3, 7));
System.out.println("phone后4位:" + phone.substring(7, phone.length()));
System.out.println("phone中4*:" + phoneNumber);
结果为:
phone总长度:11
phone前3位:131
phone中4位:2345
phone后4位:6789
phone中4*:131****6789
phone = "13123456789";
phoneNumber = phone.replaceAll("(\\d{3})\\d{4}(\\d{4})", "$1****$2");
System.out.println("正则phone中4*:" + phoneNumber);
String idCard = "123456789987654321";
//$1、$2、……表示正则表达式里面第一个、第二个、……括号里面的匹配内容
String idCardNumber = idCard.replaceAll("(\\d{4})\\d{10}(\\w{4})", "$1****$2");
System.out.println("身份证号长度:" + idCard.length());
System.out.println("正则idCard中4*:" + idCardNumber);
正则phone中4*:131****6789
身份证号长度:18
正则idCard中4*:1234****4321
org.apache.commons
commons-lang3
3.7
这里主要用StringUtils工具的overlay(String str,String overlay,int start,int end)方法可以在指定位置进行字符序列替换,从start索引处开始(包含)到end-1索引处为止进行替换
import org.apache.commons.lang3.StringUtils;
/**
* @Author: liangbl
* @Date: 2019/5/7 14:05
* @Description: 数据隐私显示 手机号,身份证号和银行卡号等
*/
public class PrivacyDimmer {
private static final String OVERLAY = "****";
private static final int START = 3;
private static final int END = 7;
/**
* 139****0504
*
* @param content
* @return
*/
public static String maskMobile(String content) {
if (StringUtils.isEmpty(content)) {
return "";
}
return StringUtils.overlay(content, OVERLAY, START, END);
}
/**
* 过滤邮箱账号
* 132****99308084911
*
* @param email
* @return
*/
public static String maskEmail(String email) {
if (StringUtils.isEmpty(email)) {
return "";
}
String at = "@";
if (!email.contains(at)) {
return email;
}
/**
* 这里主要逻辑是需要保留邮箱的注册商 比如@qq.com
*/
int length = StringUtils.indexOf(email, at);
String content = StringUtils.substring(email, 0, length);
String mask = StringUtils.overlay(content, OVERLAY, START, END);
return mask + StringUtils.substring(email, length);
}
/**
* 身份证打码操作
* 132****99308084911
*
* @param idCard
* @return
*/
public static String maskIdCard(String idCard) {
if (StringUtils.isEmpty(idCard)) {
return "";
}
return StringUtils.overlay(idCard, OVERLAY, START, END);
}
}
@Test
public void overlay() {
logger.info("手机号 {}", PrivacyDimmer.maskMobile("13661014724"));
logger.info("手机号 {}", PrivacyDimmer.maskMobile("13"));
logger.info("邮箱 {}", PrivacyDimmer.maskEmail("[email protected]"));
logger.info("邮箱不够四位 {}", PrivacyDimmer.maskEmail("[email protected]"));
logger.info("邮箱错误 {}", PrivacyDimmer.maskEmail("22qq.com"));
logger.info("假身份证号 {}",PrivacyDimmer.maskIdCard("132128199308084911"));
}
20:17:51.730 [main] INFO com.lockbur.open.AppTest - 手机号 136****4724
20:17:51.740 [main] INFO com.lockbur.open.AppTest - 手机号 13****
20:17:51.741 [main] INFO com.lockbur.open.AppTest - 邮箱 845****[email protected]
20:17:51.741 [main] INFO com.lockbur.open.AppTest - 邮箱不够四位 22****@qq.com
20:17:51.742 [main] INFO com.lockbur.open.AppTest - 邮箱错误 22qq.com
20:17:51.742 [main] INFO com.lockbur.open.AppTest - 假身份证号 132****99308084911
参考:http://www.cnblogs.com/wangzn/p/7210295.html
https://blog.csdn.net/WK313753744/article/details/87867720