-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathStringUtils.java
More file actions
48 lines (43 loc) · 1.11 KB
/
StringUtils.java
File metadata and controls
48 lines (43 loc) · 1.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package com.space.utils;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* 字符串工具类
* @author zhuzhe
* @date 2018/5/3 13:43
*/
public abstract class StringUtils extends org.apache.commons.lang3.StringUtils {
/**
* str 为 null 或 "" 时返回false,否则返回true
* @param str
* @return
*/
public static boolean isNotEmpty(String str) {
return !isEmpty(str);
}
/**
* str == null 返回 ""
* str == "" 返回 ""
* str == " abc " 返回 "abc"
* str == "a bc" 返回 "a bc"
* @param str
* @return
*/
public static String trimToEmpty(String str) {
if (isEmpty(str)){
return "";
}
return str.trim();
}
/**
* 使用正则表达式验证字符串
* @param checkTarget 验证的目标
* @param checkRegex 验证规则,是一个正则表达式
* @return 验证成功返回true,否则返回false
*/
public static boolean checkWithRegex(Object checkTarget,String checkRegex) {
Matcher matcher = Pattern.compile(checkRegex)
.matcher(StringUtils.trimToEmpty(String.valueOf(checkTarget)));
return matcher.matches();
}
}