这篇文章将为大家详细讲解有关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;
}
关于Java中如何使用掩码就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。