自定义Java工具类
我们平时在做项目的时候经常会做一下一些校验,比如(手机、邮箱、身份证号格式校验;字符串和集合等是否为空判断),在这里我们讲介绍一些常见的数据校验,我们把它封装在utils类中,方便以后做项目去调用。
/*判断字符串是否为空,这里为空的标准为字符串等于null 或者去除开头结尾的空值字符长度为0*/
public static boolean isEmpty(String str) {
return str == null || str.trim().length() == 0;
}
/*判断collection集合和其子类是否为空,在这里都可以用这个方法 判断标准为集合为null或者集合中没有元素 */
@SuppressWarnings("rawtypes")
public static boolean isEmpty(Collection collection) {
return collection == null || collection.size() < 1;
}
/*判断map集合和其子类是否为空,在这里都可以用这个方法 判断标准为集合为null或者集合中没有元素 */
@SuppressWarnings("rawtypes")
public static boolean isEmpty(Map map) {
return map == null || map.size() < 1;
}
/*通过正则表达式判断该字符串是否为邮箱格式*/
public static boolean isEmail(String email) {
if (email == null) {
return false;
}
/*判断邮箱格式: ^\\w+:邮箱可以以数字或字母开始,出现一次或者多次 ([-+.]\\w+)*:(后面可以跟着-号、+号或者.号在拼接数字或字母一次 或多次),括号中的内容可以出现零次或者多次 @:中间拼接上@符号 \\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$:这部分和前面部分类似 例如邮箱:abc-def+ghi.jkl.mno@pq.com.cn 因为不同的邮箱服务器,域名的命名规则是有差异的,这个是比较通用的一种 */
return Pattern.matches("^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$", email);
}
/*通过正则表达式判断该字符串是否为手机格式*/
public static boolean isPhone(String phoneNum) {
if (phoneNum == null) {
return false;
}
//验证规则,手机号第一位为1,后面十位为数字
return Pattern.matches("^1(\\d{10})$", phoneNum);
}
/*通过正则表达式判断该字符串是否为身份证号码格式*/
/* 身份证分为15位身份证和18位身份证号码 18位身份证号:xxxxxx yyyy MM dd *** 0 十八位 15位身份证号:xxxxxx yy MM dd ** 0 十五位 首先身份证是以1-9开头,前6位表示区域:^[1-9]/d{5} 年的前两位:(18|19|([23]\d)) 年的后两位: \d{2} 月份:((0[1-9])|(10|11|12)) 天:(([0-2][1-9])|10|20|30|31) 三位顺序码:\d{3} 两位顺序码:\d{2} 校验码:[0-9Xx] 十八位: ^[1-9]\d{5}(18|19|([23]\d))\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$ 十五位: ^[1-9]\d{5}\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{2}$ */
public static boolean isIdCard(String idCard) {
if (idCard == null) {
return false;
}
return Pattern.matches("(^[1-9]\d{5}(18|19|([23]\d))\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$)|(^[1-9]\d{5}\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{2}$)", idCard);
}
还没有评论,来说两句吧...