当前位置:网站首页>工具笔记 —— 常用自定义工具类(正则,随机数等)
工具笔记 —— 常用自定义工具类(正则,随机数等)
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 "";
}
边栏推荐
- Encryptor and client authenticate with each other
- Alibaba cloud development board haas510 responds to UART serial port instructions
- Does jupyternotebook have a Chinese character database. Can you recognize handwritten Chinese in deep learning
- 2064: [example 2.1] exchange value
- How to brush leetcode
- Use of awlive structures
- Bridging and net
- 618进入后半段,苹果占据高端市场,国产手机终于杀价竞争
- Hash tables, sets, maps, trees, heaps, and graphs
- 通过loganalyzer展示数据库中的日志
猜你喜欢

Alibaba cloud development board haas510 connects to the Internet of things platform -- Haas essay solicitation
颜色编码格式介绍

一种快速创建测试窗口的方法

拆改廣告機---業餘解壓

Acwing: topology sequence

Alibaba cloud development board haas510 parses serial port JSON data and sends attributes

xcode 调试openGLES

Encryptor and client authenticate with each other

go-zero 微服务实战系列(二、服务拆分)

Web3.0, the era of "stimulating creativity"
随机推荐
Binary tree traversal
Install RPM package offline using yum
阿裏雲開發板HaaS510報送設備屬性
【mysql进阶】查询优化原理与方案(六)
Cmake basic tutorial - 01 a-hello-cmake
简述CGI与FASTCGI区别
Use of awlive structures
Codeforces 1638 A. reverse - simple thinking
通过loganalyzer展示数据库中的日志
【mysql进阶】索引分类及索引优化方案(五)
Codeforces 1629 D. pecuriar movie preferences - simple thinking, palindrome strings
Cdeforces 1638 C. inversion graph - simple thinking
阿里云开发板HaaS510连接物联网平台--HaaS征文
Implementation of Ackermann function with simulated recursion
xcode 调试openGLES
After reading the question, you will point to offer 16 Integer power of numeric value
Understanding recursion
Codeforces 1637 A. sorting parts - simple thinking
Time processing in C language (conversion between string and timestamp)
Application of short circuit expression (||) in C language