JAVA脱敏方法

脱敏方法

public static String hideMiddel(String validateStr, int startNum, int endNum, int middle){
    //不允许为空
    if(StringUtils.isEmpty(validateStr)){
        return null;
    }
    int strLength = validateStr.length();
    //长度判断
    if (startNum < 0 || startNum > strLength) {
        return validateStr;
    }
    if (endNum < 0 || endNum > strLength) {
        return validateStr;
    }
    if (startNum + endNum > strLength) {
        return validateStr;
    }
    //获得头
    String start = validateStr.substring(0, startNum);
    //获得尾
    String end = validateStr.substring(strLength - endNum, strLength);
    //获得中间*
    StringBuilder middleStr = new StringBuilder();
    if (middle > 0) {
        for (int i = 0; i < middle; i++) {
            middleStr.append("*");
        }
    } else {
        for (int i = 0; i < strLength-startNum-endNum; i++) {
            middleStr.append("*");
        }
    }
    //拼接返回
    return start + middleStr + end;
}
  • validateStr
    String
    脱敏的字符串
  • startNum
    int
    从左到右要展示的位数(可为0)
  • endNum
    int
    从右到左要展示的位数(可为0)
  • middle
    int
    中间*的个数(可为0)

你可能感兴趣的:(java,笔记,java,字符串)