Java掩码的几种使用例举
更新时间:2019年03月14日 10:01:56 作者:Alan_阿兰
今天小编就为大家分享一篇关于Java掩码的使用,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
java掩码
private static String nameMask(String name) throws Exception {
if(name ==null)throw new Exception("请输入要掩码的字符串");
if(name.length()<=1) return name+"*";
return name.replaceAll("([\\u4e00-\\u9fa5]{1})(.*)", "$1"+createAsterisk(name.length()-1));
}
private static String createAsterisk(int len) {
StringBuffer sb = new StringBuffer();
for(int i=0;i<len;i++){
sb.append("*");
}
return sb.toString();
}
/**
* 对客户证件号码做掩码
*
* */
public static String maskCertId(String certId) throws Exception
{
if(certId==null||certId.length()==0) return "";
if(certId.length()==18)
{
String v = certId.substring(0,4);
String end = certId.substring(certId.length()-4);
return v+StringUtils.repeat("*",8)+end;
}
else
return "";
}
/**
* 对客户姓名做掩码
* @throws JBOException
* */
public static String maskUserName(String userName) throws Exception
{
if(userName==null||userName.length()==0) return "";
String v = userName.substring(0,1);
return StringUtils.rightPad(v, userName.length(),"*");//StringUtils.rightPad方法做一个字符串右补齐
}
/**
* 对字符串进行脱敏处理
* @param word 被脱敏的字符
* @param startLength 被保留的开始长度 0代表不保留
* @param endLength 被保留的结束长度 0代表不保留
* @param pad 填充字符
* */
public static String wordMask(String word,int startLength ,int endLength,String pad) {
if(word==null) return StringUtils.leftPad("", startLength+endLength,pad);
if(word.length()<=startLength+endLength) return StringUtils.leftPad("", startLength+endLength,pad);
String startStr = "";
String endStr = "";
int padLength = 0;
if(word.length()>startLength) startStr = StringUtils.substring(word, 0,startLength);
if(word.length()>startLength+endLength) endStr = StringUtils.substring(word, word.length()-endLength);
padLength = word.length()-startLength-endLength;
return startStr + StringUtils.repeat(pad, padLength)+endStr;
}
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对脚本之家的支持。如果你想了解更多相关内容请查看下面相关链接
相关文章
解决RestTemplate 的getForEntity调用接口乱码的问题
这篇文章主要介绍了解决RestTemplate 的getForEntity调用接口乱码的问题,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教2021-08-08
Springboot之@ConfigurationProperties注解解读
在Spring Boot中,@EnableConfigurationProperties注解的主要作用是激活@ConfigurationProperties注解的配置属性类,从而让配置属性类能被Spring容器管理,这样的话,我们就可以在属性类中轻松地使用@ConfigurationProperties来绑定配置文件中的属性2024-10-10
SpringCloud Alibaba 基本开发框架搭建过程
这篇文章主要介绍了SpringCloud Alibaba 基本开发框架搭建过程,开发工具选用的idea,本文通过图文实例相结合给大家分享搭建全过程,需要的朋友可以参考下2021-06-06


最新评论