public class StartUtils {
/**
* 掩码转换规则
* @param len
* @return
*/
public static Map<Integer, String> convertToStartRule(int len) {
Map<Integer, String> m = new HashMap<Integer, String>();
String indexRule ="";
switch (len) {
case 9:
{
indexRule="4|1";
break;
}
case 10:
{
indexRule="3|3";
break;
}
case 11:
{
indexRule="3|4";
break;
}
case 12:
{
indexRule="4|4";
break;
}
case 13:
{
indexRule="4|5";
break;
}
case 14:
{
indexRule="4|6";
break;
}
case 15:{
indexRule="4|7";
break;
}
case 16:
{
indexRule="4|8";
break;
}
case 17:
{
indexRule="4|9";
break;
}
case 18:
{
indexRule="4|10";
break;
}
case 19: {
indexRule="4|11";
break;
}
default:{
if(len>3) {
int mol = len % 4;
int s=len/4;
indexRule=s+"|"+(2*s+mol);
}else {
indexRule="0|1";
}
break;
}
}
m.put(len,indexRule);
return m;
}
/**
* 转换成掩码
* @param str
* @return
*/
public static String convertToStart(String str) {
String result = str;
int len = str.length();
Map<Integer,String> mapRule=convertToStartRule(len);
String ruleStr=mapRule.get(len);
String[] ruleArr=ruleStr.split("\\|");
int frontIndex=Integer.parseInt( ruleArr[0]);
int subLength=Integer.parseInt(ruleArr[1]);
String frontStr= str.substring(0,frontIndex);
String backStr=str.substring(frontIndex+subLength,len);
result=frontStr+printStart(subLength)+backStr;
return result;
}
/**
* 打印掩码个数
* @param subLength
* @return
*/
public static String printStart(int subLength) {
String start = "";
for(int i=0;i<subLength;i++){
start += "*";
}
return start;
}
}
手机号脱敏升级版:
/**
* 手机号脱敏
*/
public static String phoneEncode(String phone) {
if (StringUtils.isEmpty(phone)) {
return "";
}
return phone.replaceAll("(\\d{3})\\d{4}(\\d{4})", "$1****$2");
}