java对地址字符串脱敏

脱敏规则

数字个位数不显示,2位及以上显示最后1位数字,有中文字全部显示

如:22弄22号20层2003室→*2弄*2号*0层**3室

22弄1号20层2003室→*2弄*号*0层**3室

1弄1号1层1室→*弄*号*层*室

    




/**

     * 脱敏地址字符串中的数字
     * @param address
     * @return
     */
    public static String addressTM(String address) {
if(address == null) {
return "";
}
char[] aa = address.toCharArray();
String newAddr = "";
String temp = "";
for(int a = aa.length - 1; a >= 0; a--) {
if((int)aa[a] >= '0' && (int)aa[a] <= '9') {
temp = aa[a] + temp;
} else {
if(temp.length() > 0) {
int l = 2;
newAddr = temp.substring((temp.length() < l ? l : temp.length()) - 1, temp.length()) + newAddr;
if(temp.length() - 1<1){
newAddr = "*" + newAddr;
}else{
for(int b = 0; b < temp.length() - 1; b++) {
newAddr = "*" + newAddr;
}
}
}
temp = "";
newAddr = aa[a] + newAddr;
}
}
//武胜路333号1层
Pattern p = Pattern.compile(".*\\d+.*");  
Matcher m = p.matcher(temp);  
if(m.matches()){
if(temp.length()>1){
newAddr="*"+temp.substring(temp.length()-1,temp.length())+newAddr;
for (int i = 0; i < temp.length()-2; i++) {
newAddr="*"+newAddr;
}
}else{
newAddr="*"+newAddr;
}
}
return newAddr;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值