1、生成一些随机数字组合
public static String getNum(int length) { String val = ""; Random random = new Random(); for (int i = 0; i < length; i++) { val += String.valueOf(random.nextInt(10)); } return val; }
2、生成随机数和字母
public static String getCharAndNum(int length){ String val = ""; Random random = new Random(); for(int i=0;i<length;i++){ String charorNum = random.nextInt(2)% 2 ==0?"char":"num"; if("char".equalsIgnoreCase(charorNum)){ int choice = random.nextInt(2) % 2 ==0 ? 65 : 97; val +=(char) (choice + random.nextInt(26)); }else if("num".equalsIgnoreCase(charorNum)){ val +=String.valueOf(random.nextInt(10)); } } return val; }
3、对姓名进行隐藏
public static String hideName(String name) { if (name!=null) { String newName = name.substring(0, 1); int asteriskLen = name.length() - 1; for(int i=0;i<asteriskLen;i++){ newName += "*"; } return newName; } return name; }
4、对手机号码进行隐藏中间的几位
public static String hidePhone(String phone){ if(phone!="" && phone.length() == 11){ return phone.substring(0, 3) + "*****" + phone.substring(8); } return phone; }
5、对身份证号码进行隐藏处理
public static String hideIdCard(String idCard){ if(idCard!=null && idCard.length()>7){ String part1 = idCard.substring(0,3); String part2 = idCard.substring(idCard.length()-4); StringBuffer part3 = new StringBuffer(); for(int i=0;i<idCard.length()-7;i++){ part3.append("*"); } return part1+part3+part2; } return idCard; }