提交 | 用户 | age
|
5c5945
|
1 |
package com.hx.util; |
E |
2 |
|
|
3 |
import java.io.UnsupportedEncodingException; |
|
4 |
import java.text.MessageFormat; |
|
5 |
import java.util.Map; |
|
6 |
import java.util.regex.Matcher; |
|
7 |
import java.util.regex.Pattern; |
|
8 |
|
|
9 |
/** |
|
10 |
* 字符串工具类 |
|
11 |
* |
|
12 |
* @author ThinkGem |
|
13 |
* @version 2013-05-22 |
|
14 |
*/ |
|
15 |
public class StringUtils { |
|
16 |
|
|
17 |
private static final char SEPARATOR = '_'; |
|
18 |
private static final String CHARSET_NAME = "UTF-8"; |
|
19 |
|
|
20 |
/** |
|
21 |
* 格式化字符串,替换原字符串中的{0}, {1},按顺序替换 |
|
22 |
* |
|
23 |
* @param oriStr |
|
24 |
* @param args |
|
25 |
* @return |
|
26 |
*/ |
|
27 |
public static String format(String oriStr, Object... args) { |
|
28 |
return MessageFormat.format(oriStr, args); |
|
29 |
} |
|
30 |
|
|
31 |
/** |
|
32 |
* 转换为字节数�? |
|
33 |
* |
|
34 |
* @param str |
|
35 |
* @return |
|
36 |
*/ |
|
37 |
public static byte[] getBytes(String str) { |
|
38 |
if (str != null) { |
|
39 |
try { |
|
40 |
return str.getBytes(CHARSET_NAME); |
|
41 |
} catch (UnsupportedEncodingException e) { |
|
42 |
return null; |
|
43 |
} |
|
44 |
} else { |
|
45 |
return null; |
|
46 |
} |
|
47 |
} |
|
48 |
|
|
49 |
/** |
|
50 |
* 只取后两�? |
|
51 |
* |
|
52 |
* @param str |
|
53 |
* @return |
|
54 |
*/ |
|
55 |
public static String getLastTwoChars(String str) { |
|
56 |
if (null != str && str.trim().length() > 0) { |
|
57 |
str = str.trim(); |
|
58 |
int len = str.length(); |
|
59 |
if (len == 2) { |
|
60 |
return str; |
|
61 |
} else if (len > 2) { |
|
62 |
return str.substring(len - 2); |
|
63 |
} |
|
64 |
} |
|
65 |
return ""; |
|
66 |
} |
|
67 |
|
|
68 |
/** |
|
69 |
* 只取后三�? |
|
70 |
* |
|
71 |
* @param str |
|
72 |
* @return |
|
73 |
*/ |
|
74 |
public static String getLastThreeChars(String str) { |
|
75 |
if (null != str && str.trim().length() > 0) { |
|
76 |
str = str.trim(); |
|
77 |
int len = str.length(); |
|
78 |
if (len == 3) { |
|
79 |
return str; |
|
80 |
} else if (len > 3) { |
|
81 |
return str.substring(len - 3); |
|
82 |
} |
|
83 |
} |
|
84 |
return ""; |
|
85 |
} |
|
86 |
|
|
87 |
/** |
|
88 |
* 替换为手机识别的HTML,去掉样式及属�?�,保留回车�? |
|
89 |
* |
|
90 |
* @param html |
|
91 |
* @return |
|
92 |
*/ |
|
93 |
public static String replaceMobileHtml(String html) { |
|
94 |
if (html == null) { |
|
95 |
return ""; |
|
96 |
} |
|
97 |
return html.replaceAll("<([a-z]+?)\\s+?.*?>", "<$1>"); |
|
98 |
} |
|
99 |
|
|
100 |
/** |
|
101 |
* 驼峰命名法工�? |
|
102 |
* |
|
103 |
* @return toCamelCase("hello_world") == "helloWorld" |
|
104 |
* toCapitalizeCamelCase("hello_world") == "HelloWorld" |
|
105 |
* toUnderScoreCase("helloWorld") = "hello_world" |
|
106 |
*/ |
|
107 |
public static String toCamelCase(String s) { |
|
108 |
if (s == null) { |
|
109 |
return null; |
|
110 |
} |
|
111 |
|
|
112 |
s = s.toLowerCase(); |
|
113 |
|
|
114 |
StringBuilder sb = new StringBuilder(s.length()); |
|
115 |
boolean upperCase = false; |
|
116 |
for (int i = 0; i < s.length(); i++) { |
|
117 |
char c = s.charAt(i); |
|
118 |
|
|
119 |
if (c == SEPARATOR) { |
|
120 |
upperCase = true; |
|
121 |
} else if (upperCase) { |
|
122 |
sb.append(Character.toUpperCase(c)); |
|
123 |
upperCase = false; |
|
124 |
} else { |
|
125 |
sb.append(c); |
|
126 |
} |
|
127 |
} |
|
128 |
|
|
129 |
return sb.toString(); |
|
130 |
} |
|
131 |
|
|
132 |
/** |
|
133 |
* 驼峰命名法工�? |
|
134 |
* |
|
135 |
* @return toCamelCase("hello_world") == "helloWorld" |
|
136 |
* toCapitalizeCamelCase("hello_world") == "HelloWorld" |
|
137 |
* toUnderScoreCase("helloWorld") = "hello_world" |
|
138 |
*/ |
|
139 |
public static String toCapitalizeCamelCase(String s) { |
|
140 |
if (s == null) { |
|
141 |
return null; |
|
142 |
} |
|
143 |
s = toCamelCase(s); |
|
144 |
return s.substring(0, 1).toUpperCase() + s.substring(1); |
|
145 |
} |
|
146 |
|
|
147 |
/** |
|
148 |
* 驼峰命名法工�? |
|
149 |
* |
|
150 |
* @return toCamelCase("hello_world") == "helloWorld" |
|
151 |
* toCapitalizeCamelCase("hello_world") == "HelloWorld" |
|
152 |
* toUnderScoreCase("helloWorld") = "hello_world" |
|
153 |
*/ |
|
154 |
public static String toUnderScoreCase(String s) { |
|
155 |
if (s == null) { |
|
156 |
return null; |
|
157 |
} |
|
158 |
|
|
159 |
StringBuilder sb = new StringBuilder(); |
|
160 |
boolean upperCase = false; |
|
161 |
for (int i = 0; i < s.length(); i++) { |
|
162 |
char c = s.charAt(i); |
|
163 |
|
|
164 |
boolean nextUpperCase = true; |
|
165 |
|
|
166 |
if (i < (s.length() - 1)) { |
|
167 |
nextUpperCase = Character.isUpperCase(s.charAt(i + 1)); |
|
168 |
} |
|
169 |
|
|
170 |
if ((i > 0) && Character.isUpperCase(c)) { |
|
171 |
if (!upperCase || !nextUpperCase) { |
|
172 |
sb.append(SEPARATOR); |
|
173 |
} |
|
174 |
upperCase = true; |
|
175 |
} else { |
|
176 |
upperCase = false; |
|
177 |
} |
|
178 |
|
|
179 |
sb.append(Character.toLowerCase(c)); |
|
180 |
} |
|
181 |
|
|
182 |
return sb.toString(); |
|
183 |
} |
|
184 |
|
|
185 |
/** |
|
186 |
* 根据规则regExpr,判断字符内容是否匹配 |
|
187 |
* <p> |
|
188 |
* 利用正则表达式规则判断,待验证的字符内容是否符合要求�? <br> |
|
189 |
* 比如'[0-9]*'即判断是否为数字的规�?, '^[a-z0-9_-]{3,15}$'即判断是否为用户名的规则,<br> |
|
190 |
* '((?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$%]).{6,20})'即判断是否为密码的规�?,<br> |
|
191 |
* '^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$'即email规则,<br> |
|
192 |
* '([^\s]+(\.(?i)(jpg|png|gif|bmp))$)'即判断某图片文件格式,<br> |
|
193 |
* '^([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\.([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\.([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\.([01]?\\d\\d?|2[0-4]\\d|25[0-5])$'即IP规则. |
|
194 |
* |
|
195 |
* |
|
196 |
* @param regExpr |
|
197 |
* 正则表达�? |
|
198 |
* @param content |
|
199 |
* 待验证的字符内容 |
|
200 |
* @return boolean |
|
201 |
*/ |
|
202 |
public static boolean isMatch(String regExpr, String content) { |
|
203 |
if (StringUtils.isEmpty(regExpr, content)) |
|
204 |
return false; |
|
205 |
|
|
206 |
Pattern pattern = Pattern.compile(regExpr); |
|
207 |
Matcher matcher = pattern.matcher(content); |
|
208 |
|
|
209 |
return matcher.matches(); |
|
210 |
} |
|
211 |
|
|
212 |
/** |
|
213 |
* 正则判断纯数�? |
|
214 |
* |
|
215 |
* @param str |
|
216 |
* @return |
|
217 |
*/ |
|
218 |
public static boolean isNumeric(String str) { |
|
219 |
Pattern pattern = Pattern.compile("[0-9]*"); |
|
220 |
Matcher isNum = pattern.matcher(str); |
|
221 |
if (!isNum.matches()) { |
|
222 |
return false; |
|
223 |
} |
|
224 |
return true; |
|
225 |
} |
|
226 |
|
|
227 |
public static boolean isEmpty(String str) { |
|
228 |
return null == str || str.trim().length() == 0 || "undefined".equals(str); |
|
229 |
} |
|
230 |
|
8bc3c9
|
231 |
/**判断空*/ |
C |
232 |
public static boolean isNull(String str) { |
|
233 |
return null == str || str.trim().length() == 0 || "undefined".equals(str); |
|
234 |
} |
|
235 |
|
|
236 |
/**判断非空*/ |
|
237 |
public static boolean noNull(String str) { |
|
238 |
return null != str && str.trim().length() != 0 && !"undefined".equals(str); |
|
239 |
} |
|
240 |
|
5c5945
|
241 |
/** |
E |
242 |
* 判断字符内容中是否存在一个为空的字符�? |
|
243 |
* <p> |
|
244 |
* 如果在所有的字符串内容中,只要存在某�?个字符串为空,则返回为true, 如果�?有的字符串内容中,不存在�?个空字符串,则返回为false. |
|
245 |
* |
|
246 |
* @param strings |
|
247 |
* @return boolean |
|
248 |
*/ |
|
249 |
public static boolean isEmpty(String... strings) { |
|
250 |
if (strings == null) |
|
251 |
return true; |
|
252 |
|
|
253 |
for (String str : strings) { |
|
254 |
if (StringUtils.isEmpty(str)) |
|
255 |
return true; |
|
256 |
} |
|
257 |
|
|
258 |
return false; |
|
259 |
} |
|
260 |
|
|
261 |
public static boolean isAllEmpty(String... strings) { |
|
262 |
if (strings == null) |
|
263 |
return true; |
|
264 |
|
|
265 |
for (String str : strings) { |
|
266 |
if (!StringUtils.isEmpty(str)) |
|
267 |
return false; |
|
268 |
} |
|
269 |
|
|
270 |
return true; |
|
271 |
} |
|
272 |
|
|
273 |
/** |
|
274 |
* @param strings |
|
275 |
* 该数组是否有�? |
|
276 |
* @return |
|
277 |
*/ |
|
278 |
public static boolean hasContent(String[] strings) { |
|
279 |
if (strings != null && strings.length > 0) |
|
280 |
return true; |
|
281 |
|
|
282 |
return false; |
|
283 |
} |
|
284 |
|
|
285 |
/** |
|
286 |
* @param params |
|
287 |
* 该map是否有�?? |
|
288 |
* @return |
|
289 |
*/ |
|
290 |
public static boolean hasContent(Map<?, ?> params) { |
|
291 |
if (params != null && params.size() > 0) |
|
292 |
return true; |
|
293 |
|
|
294 |
return false; |
|
295 |
} |
|
296 |
|
|
297 |
public static boolean isNumeric2(String str) { |
|
298 |
if (null == str || isEmpty(str)) { |
|
299 |
return false; |
|
300 |
} |
|
301 |
char[] chars = str.toCharArray(); |
|
302 |
for (char c : chars) { |
|
303 |
if (!Character.isDigit(c)) |
|
304 |
return false; |
|
305 |
} |
|
306 |
return true; |
|
307 |
} |
|
308 |
|
|
309 |
public static int parseInt(String string) { |
|
310 |
if (null != string && isNumeric2(string)) { |
|
311 |
return Integer.parseInt(string); |
|
312 |
} |
|
313 |
return 0; |
|
314 |
} |
|
315 |
|
c38541
|
316 |
/** |
E |
317 |
* 对字符串(手机,名称)作隐藏处理 |
|
318 |
* @param oriStr 原始字符串 |
|
319 |
* @param type 类型0:只保留第一个及最后一个字符,其它用*替代;1前4后3保留,中间用*替代 |
|
320 |
* @return |
|
321 |
*/ |
|
322 |
public static String hideKeyWord(String oriStr, int type) |
|
323 |
{ |
|
324 |
if(!isEmpty(oriStr)) |
|
325 |
{ |
|
326 |
String temp = null; |
|
327 |
|
|
328 |
if(type == 0) |
|
329 |
{ |
|
330 |
if(oriStr.length() == 1) |
|
331 |
{ |
|
332 |
temp = oriStr; |
|
333 |
}else if(oriStr.length() == 2) { |
|
334 |
temp = oriStr.substring(0, 1) + "*"; |
|
335 |
}else if(oriStr.length() > 2) { |
|
336 |
temp = oriStr.substring(0, 1); |
|
337 |
for (int i = 1; i < oriStr.length() - 1; i++) { |
|
338 |
temp += "*"; |
|
339 |
} |
|
340 |
temp += oriStr.substring(oriStr.length() - 1, oriStr.length()); |
|
341 |
} |
|
342 |
}else{ |
|
343 |
if(oriStr.length() < 8) |
|
344 |
{ |
|
345 |
temp = oriStr; |
|
346 |
}else{ |
|
347 |
temp = oriStr.substring(0,3); |
|
348 |
for (int i = 3; i < oriStr.length() - 4; i++) { |
|
349 |
temp += "*"; |
|
350 |
} |
|
351 |
temp += oriStr.substring(oriStr.length() - 4, oriStr.length()); |
|
352 |
} |
|
353 |
} |
|
354 |
|
|
355 |
return temp; |
|
356 |
} |
|
357 |
|
|
358 |
return oriStr; |
|
359 |
} |
|
360 |
|
691dee
|
361 |
/** |
A |
362 |
* 去除空格 |
|
363 |
* @param info 信息 |
|
364 |
* @return 返回 |
|
365 |
*/ |
|
366 |
public static String trimBlank(String info){ |
|
367 |
if (null != info){ |
|
368 |
return info.trim(); |
|
369 |
} |
|
370 |
return null; |
|
371 |
} |
5c5945
|
372 |
} |