StrUtils.java
7.92 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
package com.server.utils;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* @author ZMS
*/
public final class StrUtils {
/**
* 是否是空白字符串
*
* @param str
* @return
*/
public static boolean isBlank(String str) {
if (str == null || str.trim().length() == 0) {
return true;
}
return false;
}
public static boolean isNotBlank(String str) {
return !isBlank(str);
}
/**
* null转换为空白字符
*
* @param str
* @return
*/
public static String null2Blank(String str) {
if (isBlank(str)) {
return "";
}
return str;
}
/**
* 首字母大写
*
* @param str
* @return
*/
public static String firstUpper(String str) {
StringBuilder ret = new StringBuilder();
if (str != null && str.length() > 0) {
ret.append(str.substring(0, 1).toUpperCase()).append(str.substring(1));
}
return ret.toString();
}
/**
* 首字母小写
*
* @param str
* @return
*/
public static String firstLower(String str) {
StringBuilder ret = new StringBuilder();
if (str != null && str.length() > 0) {
ret.append(str.substring(0, 1).toLowerCase()).append(str.substring(1));
}
return ret.toString();
}
/**
* 转换为boolean
*
* @param str
* @return
*/
public static boolean parseBoolean(String str) {
return "1".equals(str) || "true".equals(str);
}
/**
* 链接字符串
*
* @param delimiter
* @param strings
* @return
*/
public static String joinString(String delimiter, Object... strings) {
if (strings == null || strings.length == 0) {
return "";
}
StringBuilder sb = new StringBuilder();
boolean d = false;
for (Object s : strings) {
if (d) {
sb.append(delimiter);
} else {
d = true;
}
sb.append(s);
}
return sb.toString();
}
public static String joinString(String delimiter, String... strings) {
Object[] o = new Object[strings.length];
for (int i = 0; i < o.length; i++) {
o[i] = strings[i];
}
return joinString(delimiter, o);
}
/**
* 链接非空字符串
*
* @param delimiter
* @param strings
* @return
*/
public static String joinNotEmpty(String delimiter, String... strings) {
if (strings == null || strings.length == 0) {
return "";
}
StringBuilder sb = new StringBuilder();
boolean d = false;
for (String s : strings) {
if (s != null && !s.isEmpty()) {
if (d) {
sb.append(delimiter);
} else {
d = true;
}
sb.append(s);
}
}
return sb.toString();
}
/**
* 正则查找
*
* @param string
* @param reg
* @return
*/
public static String[] find(String string, String reg) {
List<String> list = new ArrayList<String>();
Pattern p = Pattern.compile(reg);
Matcher m = p.matcher(string);
while (m.find()) {
list.add(m.group());
}
return list.toArray(new String[list.size()]);
}
/**
* 统计正则个数
*
* @param string
* @param regex
* @return
*/
public static int countRegex(String string, String regex) {
if (StrUtils.isBlank(string)) {
return 0;
}
String[] para = StrUtils.find(string, regex);
return para.length;
}
public static String jsonSafe(String str) {
if (str == null) {
return "";
}
str = str.replaceAll("\n|\r|\r\n|\n\r", "");
str = str.replaceAll("\"|'", " ");
str = str.trim();
return str;
}
public static String formatLeftTime(long l) {
long[] time = DateUtils.splitTime(l);
String str = "";
if (time[0] > 0) {
str = str + time[0] + "day ";
}
if (time[1] > 0) {
str = str + time[1] + "hour ";
}
if (time[2] > 0) {
str = str + time[2] + "minute ";
}
if (time[3] > 0) {
str = str + time[3] + "second ";
}
return str;
}
public static String xssSafe(String str) {
str = str.replaceAll("<", "<");
str = str.replaceAll(">", ">");
str = str.replaceAll("\"", """);
str = str.replaceAll("'", "'");
return str;
}
public static boolean isWord(String str) {
if (str == null) {
return false;
}
return str.matches("[a-zA-Z_0-9]+");
}
public static boolean isNumber(String str) {
return str != null && str.matches("[\\-\\+]?[0-9]*\\.?[0-9]+");
}
public static boolean isInteger(String str) {
return str != null && str.matches("[\\-\\+]?[0-9]+");
}
public static String join(String[] array, String token) {
if (array == null || array.length == 0) {
return "";
}
StringBuilder sb = new StringBuilder(array[0]);
for (int i = 1; i < array.length; i++) {
sb.append(token);
sb.append(array[i]);
}
return sb.toString();
}
public static boolean requireLength(String str, int min, int max) {
return str != null && str.length() >= min && str.length() <= max;
}
public static String firstLowerCase(String str) {
return str.substring(0, 1).toLowerCase() + str.substring(1);
}
public static boolean isEmail(String str) {
return str != null && str.matches("\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*");
}
public static boolean isMobilePhone(String str) {
return str != null && str.matches("\\d{11}");
}
public static String reverse(String str) {
return new StringBuilder(str).reverse().toString();
}
/**
* 随机固定长度的数字
*
* @param length
* @return
*/
public static String randomNumber(int length) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < length; i++) {
sb.append(RandomUtils.random(10));
}
return sb.toString();
}
/**
* 随机固定长度
*
* @param length
* @return
*/
public static String randomString(int length) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < length; i++) {
int val = RandomUtils.random(36);
if (val < 10) {
sb.append(val);
} else {
char c = (char) ('a' + (val - 10));
sb.append(c);
}
}
return sb.toString();
}
public static String formatPrice(double price) {
return formatDouble(price, 2);
}
public static String formatDouble(double num, int scale) {
BigDecimal bigDecimal = new BigDecimal(num);
bigDecimal = bigDecimal.setScale(scale, RoundingMode.HALF_UP);
num = Double.parseDouble(bigDecimal.toPlainString());
int value = new Double(num).intValue();
if (num - value < Math.pow(10, -scale)) {
return String.valueOf(value);
}
return String.valueOf(num);
}
public static boolean isIpV4Address(String str) {
String reg = "([1-9]|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])(\\.(\\d|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])){3}";
return str != null && str.matches(reg);
}
public static String uuid() {
return UUID.randomUUID().toString().replace("-", "");
}
}