提交 | 用户 | 时间
|
e7c126
|
1 |
package com.iailab.framework.common.util.string; |
H |
2 |
|
|
3 |
import cn.hutool.core.text.StrPool; |
|
4 |
import cn.hutool.core.util.ArrayUtil; |
|
5 |
import cn.hutool.core.util.StrUtil; |
|
6 |
|
|
7 |
import java.util.Arrays; |
|
8 |
import java.util.Collection; |
|
9 |
import java.util.List; |
|
10 |
import java.util.Set; |
|
11 |
import java.util.stream.Collectors; |
|
12 |
|
|
13 |
/** |
|
14 |
* 字符串工具类 |
|
15 |
* |
|
16 |
* @author iailab |
|
17 |
*/ |
|
18 |
public class StrUtils { |
|
19 |
|
|
20 |
public static String maxLength(CharSequence str, int maxLength) { |
|
21 |
return StrUtil.maxLength(str, maxLength - 3); // -3 的原因,是该方法会补充 ... 恰好 |
|
22 |
} |
|
23 |
|
|
24 |
/** |
|
25 |
* 给定字符串是否以任何一个字符串开始 |
|
26 |
* 给定字符串和数组为空都返回 false |
|
27 |
* |
|
28 |
* @param str 给定字符串 |
|
29 |
* @param prefixes 需要检测的开始字符串 |
|
30 |
* @since 3.0.6 |
|
31 |
*/ |
|
32 |
public static boolean startWithAny(String str, Collection<String> prefixes) { |
|
33 |
if (StrUtil.isEmpty(str) || ArrayUtil.isEmpty(prefixes)) { |
|
34 |
return false; |
|
35 |
} |
|
36 |
|
|
37 |
for (CharSequence suffix : prefixes) { |
|
38 |
if (StrUtil.startWith(str, suffix, false)) { |
|
39 |
return true; |
|
40 |
} |
|
41 |
} |
|
42 |
return false; |
|
43 |
} |
|
44 |
|
|
45 |
public static List<Long> splitToLong(String value, CharSequence separator) { |
|
46 |
long[] longs = StrUtil.splitToLong(value, separator); |
|
47 |
return Arrays.stream(longs).boxed().collect(Collectors.toList()); |
|
48 |
} |
|
49 |
|
|
50 |
public static Set<Long> splitToLongSet(String value) { |
|
51 |
return splitToLongSet(value, StrPool.COMMA); |
|
52 |
} |
|
53 |
|
|
54 |
public static Set<Long> splitToLongSet(String value, CharSequence separator) { |
|
55 |
long[] longs = StrUtil.splitToLong(value, separator); |
|
56 |
return Arrays.stream(longs).boxed().collect(Collectors.toSet()); |
|
57 |
} |
|
58 |
|
|
59 |
public static List<Integer> splitToInteger(String value, CharSequence separator) { |
|
60 |
int[] integers = StrUtil.splitToInt(value, separator); |
|
61 |
return Arrays.stream(integers).boxed().collect(Collectors.toList()); |
|
62 |
} |
|
63 |
|
|
64 |
/** |
|
65 |
* 移除字符串中,包含指定字符串的行 |
|
66 |
* |
|
67 |
* @param content 字符串 |
|
68 |
* @param sequence 包含的字符串 |
|
69 |
* @return 移除后的字符串 |
|
70 |
*/ |
|
71 |
public static String removeLineContains(String content, String sequence) { |
|
72 |
if (StrUtil.isEmpty(content) || StrUtil.isEmpty(sequence)) { |
|
73 |
return content; |
|
74 |
} |
|
75 |
return Arrays.stream(content.split("\n")) |
|
76 |
.filter(line -> !line.contains(sequence)) |
|
77 |
.collect(Collectors.joining("\n")); |
|
78 |
} |
|
79 |
|
3f7a53
|
80 |
/** |
潘 |
81 |
* 判断字符串是不是数字 |
|
82 |
* |
|
83 |
* @param str |
|
84 |
* @return |
|
85 |
*/ |
|
86 |
public static boolean isNumeric(String str) { |
|
87 |
return str.matches("-?\\d+(\\.\\d+)?"); |
|
88 |
} |
|
89 |
|
e7c126
|
90 |
} |