/
StringUtils.java
236 lines (200 loc) · 7.13 KB
/
StringUtils.java
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
/**
* Copyright (c) 2015-2018, Michael Yang 杨福海 (fuhai999@gmail.com).
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.jboot.utils;
import com.jfinal.core.JFinal;
import com.jfinal.log.Log;
import java.io.UnsupportedEncodingException;
import java.math.BigInteger;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class StringUtils {
private static final Log log = Log.getLog(StringUtils.class);
public static String urlDecode(String string) {
try {
return URLDecoder.decode(string, JFinal.me().getConstants().getEncoding());
} catch (UnsupportedEncodingException e) {
log.error("urlDecode is error", e);
}
return string;
}
public static String urlEncode(String string) {
try {
return URLEncoder.encode(string, JFinal.me().getConstants().getEncoding());
} catch (UnsupportedEncodingException e) {
log.error("urlEncode is error", e);
}
return string;
}
public static String urlRedirect(String redirect) {
try {
redirect = new String(redirect.getBytes(JFinal.me().getConstants().getEncoding()), "ISO8859_1");
} catch (UnsupportedEncodingException e) {
log.error("urlRedirect is error", e);
}
return redirect;
}
public static boolean areNotEmpty(String... strings) {
if (strings == null || strings.length == 0)
return false;
for (String string : strings) {
if (string == null || "".equals(string)) {
return false;
}
}
return true;
}
public static boolean isNotEmpty(String string) {
return string != null && !string.equals("");
}
public static boolean areNotBlank(String... strings) {
if (strings == null || strings.length == 0)
return false;
for (String string : strings) {
if (string == null || "".equals(string.trim())) {
return false;
}
}
return true;
}
public static boolean isNotBlank(Object o) {
return o == null ? false : isNotBlank(o.toString());
}
public static boolean isNotBlank(String string) {
return string != null && !string.trim().equals("");
}
public static boolean isBlank(String string) {
return string == null || string.trim().equals("");
}
public static long toLong(String value, Long defaultValue) {
try {
if (value == null || "".equals(value.trim()))
return defaultValue;
value = value.trim();
if (value.startsWith("N") || value.startsWith("n"))
return -Long.parseLong(value.substring(1));
return Long.parseLong(value);
} catch (Exception e) {
}
return defaultValue;
}
public static int toInt(String value, int defaultValue) {
try {
if (value == null || "".equals(value.trim()))
return defaultValue;
value = value.trim();
if (value.startsWith("N") || value.startsWith("n"))
return -Integer.parseInt(value.substring(1));
return Integer.parseInt(value);
} catch (Exception e) {
}
return defaultValue;
}
public static BigInteger toBigInteger(String value, BigInteger defaultValue) {
try {
if (value == null || "".equals(value.trim()))
return defaultValue;
value = value.trim();
if (value.startsWith("N") || value.startsWith("n"))
return new BigInteger(value).negate();
return new BigInteger(value);
} catch (Exception e) {
}
return defaultValue;
}
public static boolean match(String string, String regex) {
Pattern pattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(string);
return matcher.matches();
}
public static boolean isNumeric(String str) {
if (str == null)
return false;
for (int i = str.length(); --i >= 0; ) {
int chr = str.charAt(i);
if (chr < 48 || chr > 57)
return false;
}
return true;
}
public static boolean isEmail(String email) {
return Pattern.matches("\\w+@(\\w+.)+[a-z]{2,3}", email);
}
public static boolean isMobileNumber(String phoneNumber) {
return Pattern.matches("^(1[3,4,5,7,8])\\d{9}$", phoneNumber);
}
public static String escapeHtml(String text) {
if (isBlank(text))
return text;
return text.replace("<", "<").replace(">", ">").replace("\"", """).replace("'", "'").replace("/", "/");
}
public static String uuid() {
return UUID.randomUUID().toString().replace("-", "");
}
/**
* 生成流水号
*
* @param uuid 谋订单的主键ID
* @return
*/
public static String generateSerialNumber(String uuid) {
return new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()) + Math.abs(uuid.hashCode());
}
public static String clearSpecialCharacter(String string) {
if (isBlank(string)) {
return string;
}
/**
P:标点字符;
L:字母;
M:标记符号(一般不会单独出现);
Z:分隔符(比如空格、换行等);
S:符号(比如数学符号、货币符号等);
N:数字(比如阿拉伯数字、罗马数字等);
C:其他字符
*/
// return string.replaceAll("[\\pP\\pZ\\pM\\pC]", "");
return string.replaceAll("[\\\\\'\"\\/\f\n\r\t]", "");
}
/**
* 生成验证码
*/
public static String getValidateCode() {
Random random = new Random();
return String.valueOf(random.nextInt(9999 - 1000 + 1) + 1000);//为变量赋随机值1000-9999
}
public static Set<String> splitToSet(String src, String regex) {
if (src == null) {
return null;
}
String[] strings = src.split(regex);
Set<String> set = new HashSet<>();
for (String table : strings) {
if (StringUtils.isBlank(table)) {
continue;
}
set.add(table.trim());
}
return set;
}
public static void main(String[] args) {
String url = "http://www.baidu.com?username=aaa";
System.out.println(StringUtils.urlEncode(url));
}
}