(JAVA) 电话、邮箱脱敏,带脱敏规则

不多说,直接上代码,拷走就能用:

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class PhoneOrEmailUtil {
	/**
	 * 	脱敏规则——手机号码(手机号码隐藏中间4位数)
	 * @Description: 
	 * @param telnum
	 * @return
	 * @ReturnType String
	 * @author: zzx
	 * @Created 2020年6月2日 上午10:25:10
	 */
	public static String phoneNum(String telnum){
		String rtnStr = telnum;
		if(isEmpty(rtnStr)||rtnStr.trim().length()<7){
			return rtnStr;
		}
		rtnStr = rtnStr.trim();
		// 校验是否为电话号码
		String regExp = "^1\\d{10}$";
        Pattern p = Pattern.compile(regExp);  
        Matcher m = p.matcher(rtnStr);  
        if(m.matches()){// 若判断为固话
//        	rtnStr = replace(rtnStr,rtnStr.length()-3,"****");
        	rtnStr = replace(rtnStr,4,"****");
        }
        return rtnStr;
	}
	
	/**
	 * 	脱敏规则——邮箱 (@前三位)
	 * @Description: 
	 * @param str
	 * @return
	 * @ReturnType String
	 * @author: zzx
	 * @Created 2020年6月2日 上午10:25:10
	 */
	public static String emailNum(String str){
		String rtnStr = str;
		String regExp = "^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\\.[a-zA-Z0-9_-]+)+$";
        Pattern p = Pattern.compile(regExp); 
        Matcher matcher = p.matcher(rtnStr);  
        boolean isMatched = matcher.matches(); 
		if(isMatched){
			int s =  rtnStr.indexOf("@");
			if(s >= 6) {
				return replace(rtnStr.trim(), s-3, "****");
			}else if(s >= 3 && s < 6) {
				return replace(rtnStr.trim(), s-1, "**");
			}else {
				return str;
			}
		}else{
			return rtnStr;
		}
	}
	
	/**
	 * 脱敏规则
	 * @Description: 
	 * @param str(需脱敏的字符串) 
	 * @param n(脱敏开始位)
	 * @param newChar(脱敏掩码)
	 * @return
	 * @ReturnType String
	 * @author: zzx
	 * @Created 2020年6月2日 上午10:25:10
	 */
	public static String replace (String str,int n,String newChar){ 
		String s1=""; 
		String s2=""; 
		try{
			s1=str.substring(0,n-1); 
			s2=str.substring(n-1+newChar.length(),str.length());
			return s1+newChar+s2;
		}catch(Exception ex){ 			
			return str;
		} 
	}
	
	/**
	 * 判断字符串是否为null || 空字符串
	 * 
	 * @param param
	 * @return
	 */
	public static boolean isEmpty(String param) {
		return param == null || "".equals(param.trim());
	}
	public static String replaceChar(String str) {
		String rtnStr = str;
        if(!isEmpty(rtnStr)) {
        	String regExp = "^1\\d{10}$";
        	Pattern p = Pattern.compile(regExp);  
        	Matcher m = p.matcher(rtnStr);         	
        	
        	String regExp2 = "^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\\.[a-zA-Z0-9_-]+)+$";
            Pattern p2 = Pattern.compile(regExp2); 
            Matcher matcher2 = p2.matcher(rtnStr);  
            
        	if(m.matches()) {
        		rtnStr = phoneNum(str);
        	}else if(matcher2.matches()) {
        		rtnStr = emailNum(str);
        	}        	
        }
		return rtnStr;
	}
	public static void main(String args[]) {
		String str = "xxxxxxxxxxx";
		System.out.println(phoneNum(str));
	}
}
  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

野生菠萝君

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值