当前位置:网站首页>工具笔记 —— 常用自定义工具类(正则,随机数等)
工具笔记 —— 常用自定义工具类(正则,随机数等)
2022-06-12 13:46:00 【Yuan-Programmer】
文章目录
匹配邮箱
/** * 邮箱校验 * * @param email 邮箱 * @return true or false */
public static boolean isEmail(String email) {
Matcher m = Pattern.compile("^([a-zA-Z]|[0-9])(\\w|\\-)[email protected][a-zA-Z0-9]+\\.([a-zA-Z]{2,4})$").matcher(email);
return m.matches();
}
匹配国内11位手机号码
/** * 手机号码校验 * * @param phone * @return true or false */
public static boolean isPhone(String phone) {
Matcher m = Pattern.compile("^[1][3,4,5,7,8][0-9]{9}$").matcher(phone);
return m.matches();
}
匹配5-20个只能包含数字和字母,且至少包含1个字母
/** * 长度 5-20 个字符,只能包含数字、大小写字母 且 至少包含一个字母 * * @param str 待校验字符串 * @return true or false */
public static boolean isContainOneLetter(String str) {
Matcher m = Pattern.compile("(?=.*[a-zA-Z])[a-zA-Z0-9]{5,20}").matcher(str);
return m.matches();
}
判断是否有中文(不包括中文符号)
/** * 检验字符串中是否包含中文,不包括中文字符 * * @param str 待验证字符串 * @return true-包含 false-不包含 */
public static boolean isContainsChinese(String str) {
Matcher m = Pattern.compile("[\u4e00-\u9fa5]").matcher(str);
return m.find();
}
判断某个字符是不是表情
/** * 判断某个字符是不是表情 * * @param codePoint * @return true or false */
private static boolean isEmojiCharacter(char codePoint) {
return (codePoint == 0x0) || (codePoint == 0x9) || (codePoint == 0xA)
|| (codePoint == 0xD)
|| ((codePoint >= 0x20) && (codePoint <= 0xD7FF))
|| ((codePoint >= 0xE000) && (codePoint <= 0xFFFD))
|| ((codePoint >= 0x10000) && (codePoint <= 0x10FFFF));
}
判断字符串中是否包含表情
/** * 判断字符串中是否含有表情 * * @param source * @return true or false */
public static boolean isContainsEmoji(String source) {
int len = source.length();
boolean isEmoji = false;
for (int i = 0; i < len; i++) {
char hs = source.charAt(i);
if (0xd800 <= hs && hs <= 0xdbff) {
if (source.length() > 1) {
char ls = source.charAt(i + 1);
int uc = ((hs - 0xd800) * 0x400) + (ls - 0xdc00) + 0x10000;
if (0x1d000 <= uc && uc <= 0x1f77f) {
return true;
}
}
} else {
// non surrogate
if (0x2100 <= hs && hs <= 0x27ff && hs != 0x263b) {
return true;
} else if (0x2B05 <= hs && hs <= 0x2b07) {
return true;
} else if (0x2934 <= hs && hs <= 0x2935) {
return true;
} else if (0x3297 <= hs && hs <= 0x3299) {
return true;
} else if (hs == 0xa9 || hs == 0xae || hs == 0x303d
|| hs == 0x3030 || hs == 0x2b55 || hs == 0x2b1c
|| hs == 0x2b1b || hs == 0x2b50 || hs == 0x231a) {
return true;
}
if (!isEmoji && source.length() > 1 && i < source.length() - 1) {
char ls = source.charAt(i + 1);
if (ls == 0x20e3) {
return true;
}
}
}
}
return isEmoji;
}
过滤掉字符串中的表情
/** * 过滤掉字符串中的表情 * * @param source * @return 过滤后的字符串 */
public static String filterEmoji(String source) {
if (StringUtils.isBlank(source)) {
return source;
}
StringBuilder buf = null;
int len = source.length();
for (int i = 0; i < len; i++) {
char codePoint = source.charAt(i);
if (isEmojiCharacter(codePoint)) {
if (buf == null) {
buf = new StringBuilder(source.length());
}
buf.append(codePoint);
}
}
if (buf == null) {
return source;
} else {
if (buf.length() == len) {
buf = null;
return source;
} else {
return buf.toString();
}
}
}
随机生成6位数字验证码
/** * 随机生成六位数字验证码 */
public static String randomSixCode() {
return String.valueOf(new Random().nextInt(899999) + 100000);
}
数值转换为万单位(如文章访问量等)
public static String viewNumberFormat(Integer number) {
if (number < 10000) {
return number.toString();
}else if (number <= 100000) {
return number / 1000 % 10 == 0 ? number / 10000 + "万+" : number / 10000 + "." + number / 1000 % 10 + "万+";
}else {
return number / 10000 + "万+";
}
}
日期格式转换(字符串 -> Date)
年-月-日 格式
/** * 由 Date格式日期 转换为 String格式日期 * @param date 原日期 * @param isWhole 是否全路径(true -> 年-月-日 格式,false -> 年-月-日 时:分:秒 格式) * @return 转换后的日期 */
public static String toStringFromDate(Date date, boolean isWhole) {
SimpleDateFormat format;
if (isWhole) {
format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
}else {
format = new SimpleDateFormat("yyyy-MM-dd");
}
return format.format(date);
}
日期格式转换(Date -> 字符串)
/** * 由 String格式日期 转换为 Date格式日期 * @param date 原日期 * @param isWhole 是否全路径(true -> 年-月-日 格式,false -> 年-月-日 时:分:秒 格式) * @return 转换后的日期 */
public static Date toDateFromString(String date, boolean isWhole) {
SimpleDateFormat format;
if (isWhole) {
format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
}else {
format = new SimpleDateFormat("yyyy-MM-dd");
}
Date resultDate = new Date();
try {
resultDate = format.parse(date);
} catch (ParseException e) {
e.printStackTrace();
}
return resultDate;
}
Long类型主键简单处理(异或加密)
加密
/** * * @param key 待加密字符串 (如 1524415450874658817) * @return 加密后的字符串(如 21d94c70ac5ae008746588179) */
public static String encrypt(String key) {
// 对 10 - length 长度的数字进行转换
Integer src = Integer.parseInt(key.substring(0, 10));
String timeStr = LocalDate.now().format(DateTimeFormatter.ofPattern("yyyyMMdd"));
long pwd = Long.parseLong(src + timeStr);
long xorPwd = pwd ^ Integer.parseInt(timeStr);
// 回传转换后的字符串
return Long.toHexString(xorPwd) + key.substring(10) + (key.length() - 10);
}
解密
/** * * @param key 待解密字符串 (如 21d94c70ac5ae008746588179) * @return 解密后的字符串(如 1524415450874658817) */
public static String decode(String key) {
try {
int start = key.length() - Integer.parseInt(key.substring(key.length() - 1));
// 截取 10 - length 长度进行解码
key = key.substring(0, key.length() - 1);
String src = key.substring(0, start - 1);
Long timeInt = Long.parseLong(LocalDate.now().format(DateTimeFormatter.ofPattern("yyyyMMdd")));
Long xorOrigin = (Long.parseLong(src, 16)) ^ timeInt;
Long id = (xorOrigin - timeInt) / 100000000;
// 回传解码的字符串
if (id * 100000000 == xorOrigin - timeInt) {
return id.intValue() + key.substring(start - 1);
}
}catch (StringIndexOutOfBoundsException | NumberFormatException e) {
return "";
}
return "";
}
边栏推荐
- 上海解封背后,这群开发者“云聚会”造了个AI抗疫机器人
- Code debugging - print log output to file
- Alibaba cloud development board haas510 submission device attributes
- 通过loganalyzer展示数据库中的日志
- Cdeforces 1638 C. inversion graph - simple thinking
- 【SemiDrive源码分析】【X9芯片启动流程】25 - MailBox 核间通信机制介绍(代码分析篇)之 RPMSG-IPCC RTOS & QNX篇
- 一种快速创建测试窗口的方法
- Backtracking: Prime Rings
- Record some settings for visual studio 2019
- [WUSTCTF2020]颜值成绩查询-1
猜你喜欢

阿里云开发板HaaS510将串口获取数据发送到物联网平台
![[WUSTCTF2020]颜值成绩查询-1](/img/90/e4c2882357e0a1c6a80f778887e3f5.png)
[WUSTCTF2020]颜值成绩查询-1

Briefly describe the difference between CGI and fastcgi

Web3.0,「激发创造」的时代

Implementation of Ackermann function with simulated recursion
![[WUSTCTF2020]颜值成绩查询-1](/img/dc/47626011333a0e853be87e492d8528.png)
[WUSTCTF2020]颜值成绩查询-1
![[wustctf2020] selfie score query -1](/img/90/e4c2882357e0a1c6a80f778887e3f5.png)
[wustctf2020] selfie score query -1

【视频课】android studio物联网APP设计制作全套教程--国庆期间全掌握

Xcode debugging OpenGLES

Data type conversion and conditional control statements
随机推荐
[WUSTCTF2020]颜值成绩查询-1
Application of bit operation in C language
[advanced MySQL] evolution of MySQL index data structure (IV)
Explanation of static and extern keywords
一种快速创建测试窗口的方法
Player screen orientation scheme
GPUImage链式纹理的简单实现
Cocoapods的相关知识点
Alibaba Cloud Development Board haas510 submission Device Properties
Compile and install lamp architecture of WordPress and discuz for multi virtual hosts based on fastcgi mode
280 weeks /2171 Take out the least number of magic beans
数据类型转换和条件控制语句
2066: [example 2.3] buying books
How to brush leetcode
Top 10 tips for visual studio code on Google
969. pancake sorting
【mysql进阶】索引分类及索引优化方案(五)
Web3.0, the era of "stimulating creativity"
Display logs in the database through loganalyzer
事件的传递和响应以及使用实例