/** * 字符串脱敏,保留一定的位数 * @param str 字符串 * @param count 保留位数 * @return abcdefg -> ****defg */ public static String repeat(String str, int count) { if (str == null || str.length() < count) { return str; // 如果字符串为空或长度小于count,直接返回原字符串 } // 使用StringBuilder来构建脱敏后的字符串 StringBuilder sb = new StringBuilder(str.length()); // 添加星号,长度为原字符串长度减去4 for (int i = 0; i < str.length() - count; i++) { sb.append('*'); } // 添加原字符串的最后位数字符 sb.append(str.substring(str.length() - count)); // 返回脱敏后的字符串 return sb.toString(); }
08-20
2960
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
12-07
3039
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)