import org.apache.commons.lang3.StringUtils; /** * @Author ** * @Date 2021/10/5 * @Description: */ public class DesensitizeUtil { /** * 姓名脱敏 * @param str * @param index * @return */ public static String left(String str,int index) { if (StringUtils.isBlank(str)) { return ""; } String name = StringUtils.left(str, index); return StringUtils.rightPad(name, StringUtils.length(str), "*"); } }
测试:
public class Demo01 { public static void main(String[] args) { String name = "欧阳娜娜"; String newName = DesensitizeUtil.left(name, 2); System.out.println(newName); } }
结果:欧阳**