当前位置:网站首页>Tool notes - common custom tool classes (regular, random, etc.)
Tool notes - common custom tool classes (regular, random, etc.)
2022-06-12 14:04:00 【Yuan-Programmer】
List of articles
- Match mailbox
- Match domestic 11 Phone number
- matching 5-20 Can only contain numbers and letters , And at least include 1 Letters
- Judge whether there is Chinese ( Chinese symbols are not included )
- Judge whether a character is an expression
- Determine whether the string contains an expression
- Filter out the expressions in the string
- Random generation 6 Digit verification code
- The numerical value is converted to 10000 units ( Such as article visits, etc )
- Date format conversion ( character string -> Date)
- Date format conversion (Date -> character string )
- Long Simple handling of type primary keys ( XOR Encryption )
Match mailbox
/** * Email verification * * @param email mailbox * @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();
}
Match domestic 11 Phone number
/** * Mobile number verification * * @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();
}
matching 5-20 Can only contain numbers and letters , And at least include 1 Letters
/** * length 5-20 Characters , It can only contain numbers 、 Case letters And Contains at least one letter * * @param str String to be verified * @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();
}
Judge whether there is Chinese ( Chinese symbols are not included )
/** * Verify whether the string contains Chinese , Excluding Chinese characters * * @param str String to be verified * @return true- contain false- It doesn't contain */
public static boolean isContainsChinese(String str) {
Matcher m = Pattern.compile("[\u4e00-\u9fa5]").matcher(str);
return m.find();
}
Judge whether a character is an expression
/** * Judge whether a character is an expression * * @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));
}
Determine whether the string contains an expression
/** * Judge whether there is expression in the string * * @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;
}
Filter out the expressions in the string
/** * Filter out the expressions in the string * * @param source * @return Filtered string */
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();
}
}
}
Random generation 6 Digit verification code
/** * Randomly generate six digit verification code */
public static String randomSixCode() {
return String.valueOf(new Random().nextInt(899999) + 100000);
}
The numerical value is converted to 10000 units ( Such as article visits, etc )
public static String viewNumberFormat(Integer number) {
if (number < 10000) {
return number.toString();
}else if (number <= 100000) {
return number / 1000 % 10 == 0 ? number / 10000 + " ten thousand +" : number / 10000 + "." + number / 1000 % 10 + " ten thousand +";
}else {
return number / 10000 + " ten thousand +";
}
}
Date format conversion ( character string -> Date)
year - month - Japan Format
/** * from Date Format date Convert to String Format date * @param date Original date * @param isWhole Full path or not (true -> year - month - Japan Format ,false -> year - month - Japan when : branch : second Format ) * @return Date after conversion */
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 format conversion (Date -> character string )
/** * from String Format date Convert to Date Format date * @param date Original date * @param isWhole Full path or not (true -> year - month - Japan Format ,false -> year - month - Japan when : branch : second Format ) * @return Date after conversion */
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 Simple handling of type primary keys ( XOR Encryption )
encryption
/** * * @param key Encrypted string ( Such as 1524415450874658817) * @return The encrypted string ( Such as 21d94c70ac5ae008746588179) */
public static String encrypt(String key) {
// Yes 10 - length Length of the number to convert
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);
// Returns the converted string
return Long.toHexString(xorPwd) + key.substring(10) + (key.length() - 10);
}
Decrypt
/** * * @param key String to be decrypted ( Such as 21d94c70ac5ae008746588179) * @return Decrypted string ( Such as 1524415450874658817) */
public static String decode(String key) {
try {
int start = key.length() - Integer.parseInt(key.substring(key.length() - 1));
// Intercept 10 - length Length to decode
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;
// Returns the decoded string
if (id * 100000000 == xorOrigin - timeInt) {
return id.intValue() + key.substring(start - 1);
}
}catch (StringIndexOutOfBoundsException | NumberFormatException e) {
return "";
}
return "";
}
边栏推荐
- 如果要打造品牌知名度,可以选择什么出价策略?
- [early knowledge of activities] list of recent activities of livevideostack
- Shell脚本到底是什么高大上的技术吗?
- Alibaba cloud development board haas510 submission device attributes
- 正点原子STM32F429核心板的插座型号
- 2000. reverse word prefix
- 【活动早知道】LiveVideoStack近期活动一览
- 3. Hidden processes under the ring
- Relevant knowledge points of cocoapods
- M1 pod install pod lint failure solution
猜你喜欢

通过loganalyzer展示数据库中的日志

Alibaba cloud development board haas510 sends the serial port data to the Internet of things platform

拆改廣告機---業餘解壓
![[wustctf2020] selfie score query -1](/img/dc/47626011333a0e853be87e492d8528.png)
[wustctf2020] selfie score query -1

Alibaba cloud development board haas510 connects to the Internet of things platform -- Haas essay solicitation

Acwing: topology sequence

Understanding recursion

Is MySQL query limit 1000,10 as fast as limit 10? How to crack deep paging

Xcode debugging OpenGLES

Single bus temperature sensor 18B20 data on cloud (Alibaba cloud)
随机推荐
Qt5 plug-in production
To SystemC Beginners: the first program
Running phase of SystemC
阿裏雲開發板HaaS510報送設備屬性
1414. minimum number of Fibonacci numbers with sum K
Create a slice slice pit using the make method
969. pancake sorting
Paw advanced user guide
Single bus temperature sensor 18B20 data on cloud (Alibaba cloud)
Top 10 tips for visual studio code on Google
阿里云开发板HaaS510解析串口JSON数据并发送属性
程序分析与优化 - 6 循环优化
Language skills used in development
Introduction to color coding format
Bridging and net
注重点击,追求更多用户进入网站,可以选择什么出价策略?
[wustctf2020] selfie score query -1
阿里云开发板HaaS510连接物联网平台--HaaS征文
atomic and exclusive operation
阿里云开发板HaaS510报送设备属性