给敏感字符加星号处理
支持不同长度的字符串,自动加星号处理:
/** * 敏感数据加*号处理 * * @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; }