Skip to content

Commit dabbd19

Browse files
committed
perf: 优化 StringUtil,添加 contains、subString 方法
1 parent d128d58 commit dabbd19

1 file changed

Lines changed: 47 additions & 0 deletions

File tree

simple-common/src/main/java/top/cadecode/common/util/StringUtil.java

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package top.cadecode.common.util;
22

3+
import lombok.NonNull;
4+
35
/**
46
* @author Cade Li
57
* @date 2021/7/15
@@ -24,4 +26,49 @@ public static boolean isEmpty(String str) {
2426
public static boolean isTrimEmpty(String str) {
2527
return str == null || str.trim().length() == 0;
2628
}
29+
30+
/**
31+
* 判断字符串是否包含指定子串
32+
*
33+
* @return boolean
34+
*/
35+
public static boolean contains(String str, String subStr) {
36+
if (str == null || subStr == null) {
37+
return false;
38+
}
39+
return str.contains(subStr);
40+
}
41+
42+
/**
43+
* 截取字符串,支持反序负数
44+
*
45+
* @param str 字符串
46+
* @param begin 开始位置
47+
* @return String
48+
*/
49+
public static String subString(String str, int begin) {
50+
return subString(str, begin, str.length());
51+
}
52+
53+
/**
54+
* 截取字符串,支持反序负数
55+
*
56+
* @param str 字符串
57+
* @param begin 开始位置
58+
* @param end 开始位置
59+
* @return String
60+
*/
61+
public static String subString(String str, int begin, int end) {
62+
if (str == null) {
63+
return null;
64+
}
65+
if (begin < 0) {
66+
begin = str.length() + begin;
67+
}
68+
if (end < 0) {
69+
end = str.length() + end;
70+
}
71+
return str.substring(begin, end);
72+
}
73+
2774
}

0 commit comments

Comments
 (0)