支持不同长度的字符串,自动加星号处理:

    /**
     * 敏感数据加*号处理
     *
     * @param info 要加密的字符串
     */
    public static String replaceSecretInfo(String info) {
        if (info.isEmpty()) {
            return "";
        }
        String result;
        int infoLength = info.length();
        if (infoLength == 1) {
            result = "*";
        } else if (infoLength == 2) {
            result = info.substring(0,1) + "*";
        } else {
            double tempNum = (double) infoLength / 3;
            Integer num1 = (int) Math.floor(tempNum);
            Integer num2 = (int) Math.ceil(tempNum);
            Integer num3 = infoLength - num1 - num2;
            String star = StringUtils.repeat("*", num2);
            String regex = "(.{" + num1 + "})(.{" + num2 + "})(.{" + num3 + "})";
            String replacement = "$1" + star + "$3";
            result = info.replaceAll(regex, replacement);
        }
        return result;
    }

 

相关文章:

  • 2022-12-23
  • 2021-06-09
  • 2021-09-01
  • 2022-12-23
  • 2021-12-05
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-10-11
  • 2022-12-23
  • 2021-12-26
  • 2022-12-23
  • 2021-09-22
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案