方法1:截取替换法
/**
* @description 手机处理工具
*/
public class MobileUtils {
/**
* 中间4位使用*替换
* @param phone
* @return
*/
public static String midleReplaceStar(String phone){
String result=null;
if (!StringUtils.isEmpty(phone)){
if (phone.length()<7){
result=phone;
}else{
String start = phone.substring(0,3);
String end = phone.substring(phone.length()-4,phone.length());
StringBuilder sb=new StringBuilder();
sb.append(start).append("****").append(end);
result=sb.toString();
}
}
return result;
}
public static void main(String[] args) {
MobileUtils utils=new MobileUtils();
System.out.println(MobileUtils.midleReplaceStar("12345678901"));
System.out.println(MobileUtils.midleReplaceStar("123456"));
System.out.println(MobileUtils.midleReplaceStar("1234567"));
System.out.println(MobileUtils.midleReplaceStar("1111111111111111111"));
}
}
方法2:直接替换
将字符串拆分成数组,然后将中间的替换
代码略
版权所有,欢迎保留原文链接进行转载:) 还可以关注我们的公众号