用户名用*号隐藏中间部分

/**
 * 用户名*号替换
 * @param userName
 * @return
 */
private static String replaceAction(String userName) {
    String userNameAfterReplaced = "";
    int nameLength = userName.length();
    if(nameLength<3 && nameLength>0){
        if(nameLength==1){
            userNameAfterReplaced = "*";
        }else{
            userNameAfterReplaced = userName.replaceAll(userName, "**");
        }
    }else{
        Integer num1,num2,num3;
        num2=(new Double(Math.ceil(new Double(nameLength)/3))).intValue();
        num1=(new Double(Math.floor(new Double(nameLength)/3))).intValue();
        num3=nameLength-num1-num2;
        String star= StringUtils.repeat("*",num2);
        userNameAfterReplaced = userName.replaceAll("(.{"+num1+"})(.{"+num2+"})(.{"+num3+"})","$1"+star+"$3");
    }
    return userNameAfterReplaced;
}

你可能感兴趣的