1,邮箱地址只展示部分,只展示@前面部分的第1个及最后一个字符,其它的用*代替:
public static String spliteEmail(String email) {/**传入邮箱地址*/ String newEmail = email.split("@")[0];/**获取到邮箱@前面部分*/ String[] mails = new String[newEmail.length()]; StringBuffer sb = new StringBuffer(); if (mails.length > 0) { if (mails.length < 3) {/**如果邮箱@前面字条小于3个,则显示全部完整邮箱地址*/ return email; } else {/**邮箱地址字符处理*/ for (int i = 0; i < mails.length; i++) { if (i >= 1 && i < mails.length - 1) { sb.append("*"); } else { sb.append(newEmail.charAt(i)); } } return sb.toString() + "@" + email.split("@")[1]; } } else { return ""; } }
2,手机号码处理(只显示手机号的前3及后3数字,中间用*代替):
public static String splitePhone(String phone) { String[] tel = new String[phone.length()]; StringBuffer sb = new StringBuffer(); if (tel.length > 0) { for (int i = 0; i < tel.length; i++) { if (i > 2 && i < 7) { sb.append("*"); } else { sb.append(phone.charAt(i)); } } } return sb.toString(); }另外几个常用的正则表达示:
/**@description:判断是否是身份证格式*/ public static boolean isIDCardNo(String cardNo) { Pattern pattern = Pattern.compile("(^\\d{15}$)|(^\\d{18}$)|(^\\d{17}(\\d|X|x)$)"); Matcher matcher = pattern.matcher(cardNo); return matcher.matches(); } /**判断输入的是否是中文*/ public static boolean isChineseChar(String inputString) { Pattern pattern = Pattern.compile("^[\\u4E00-\\u9FA5\\uF900-\\uFA2D]+$"); // Pattern pattern = Pattern.compile("^[\\u4E00-\\u9FA5]"); return pattern.matcher(inputString).matches(); } /**匹配非负浮点数*/ public static boolean isNotNegativeFloat(String inputString) { Pattern pattern = Pattern.compile("^[1-9]d*.d*|0.d*[1-9]d*|0?.0+|0$"); return pattern.matcher(inputString).matches(); }
时间: 2024-10-13 16:11:21