StringUtil.java
24.4 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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
package com.erry.iclear.dateInterface.util;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.StringUtils;
import javax.servlet.http.HttpServletRequest;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class StringUtil {
/**
* 正则表达式:验证邮箱
*/
private static final String REGEX_EMAIL = "^([a-z0-9A-Z_]+[-|\\.]?)+[a-z0-9A-Z_]@([a-z0-9A-Z_]+(-[a-z0-9A-Z_]+)?\\.)+[a-zA-Z_]{2,}$";
/**
* 正则表达式:验证数字
*/
private static final String REGEX_NUBMER = "\\d+\\.\\d+$|-\\d+\\.\\d+$";
/**
* 判断字符串是否为空
*
* @param src
* @return
*/
public static boolean isEmpty(String src) {
return src == null || "".equals(src.trim());
}
/**
* 判断字符串不为空
*
* @param src
* @return
*/
public static boolean isNotEmpty(String src) {
return !isEmpty(src);
}
public static String toStringExcludeNull(Object obj) {
return obj == null ? "" : obj.toString();
}
/**
* 判断字符串是否为空
*
* @param src
* @return
*/
public static boolean isEmptyWithTrim(String src) {
return src == null || "".equals(src.trim());
}
/**
* 将字符串前补0到指定长度
*
* @param value
* @param length
* @return
*/
public static String fillZero(String value, int length) {
if (value == null) {
return null;
} else {
while (value.length() < length) {
value = "0" + value;
}
}
return value;
}
/**
* 截取指定长度的字符串
*
* @param str
* @param length
* @return
*/
public static String truncate(String str, int length) {
if (!isEmpty(str) && str.length() > length) {
return str.substring(0, length);
}
return str;
}
/**
* 将首字母大写
*
* @param str
* @return
*/
public static String upcaseFirstLetter(String str) {
if (str == null) {
return null;
}
String firstChar = str.substring(0, 1);
return firstChar.toUpperCase() + str.substring(1, str.length());
}
/**
* 按指定长度format数字long
*
* @param num
* @param length
* @return
*/
public static String formatLong(long num, int length) {
String number = Long.toString(num);
while (number.length() < length) {
number = "0" + number;
}
return number;
}
/**
* 判断字符串是否为半角英文或数字
*
* @param str
* @return
*/
public static boolean isAllHalfWidth(String str) {
char[] chars = str.toCharArray();
for (int i = 0; i < chars.length; i++) {
int ascii = chars[i];
if ((ascii >= 48 && ascii <= 57) //0-9
|| (ascii >= 65 && ascii <= 90) //A-Z
|| (ascii >= 97 && ascii <= 122)) { //a-z
} else {
return false;
}
}
return true;
}
/**
* 判断字符串是否整数
*
* @param str
* @return
*/
public static boolean isNum(String str) {
if (StringUtil.isEmptyWithTrim(str)) {
return false;
}
char[] chars = str.toCharArray();
for (int i = 0; i < chars.length; i++) {
int ascii = chars[i];
if ((ascii >= 48 && ascii <= 57)) //0-9)
{
} else {
return false;
}
}
return true;
}
/**
* 判断字符串是否小数
*
* @param str
* @return
*/
public static boolean isNumeric(String str) {
Pattern pattern = Pattern.compile(REGEX_NUBMER);
Matcher isNum = pattern.matcher(str);
if (!isNum.matches()) {
return false;
}
return true;
}
public static String toString(String str) {
return str == null ? "" : str;
}
public static String toString(Object obj) {
return obj == null ? "" : obj.toString();
}
/**
* 将字符串数组按指定分隔符转为字符串
*
* @param arr
* @param seperator
* @return
*/
public static String convertArrayToString(String[] arr, String seperator) {
StringBuffer sb = new StringBuffer();
for (int i = 0; i < arr.length; i++) {
sb.append(seperator + arr[i]);
}
if (sb.length() > 0) {
sb.deleteCharAt(0);
}
return sb.toString();
}
/**
* Return the string of exception
*
* @param t
* @return
* @throws IOException
*/
public static String exceptionToString(Throwable t) throws IOException {
if (t == null) {
return null;
}
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
t.printStackTrace(new PrintStream(baos));
} finally {
baos.close();
}
String sb = baos.toString();
if (sb.length() > 5000) {
return sb.substring(0, 5000);
}
return sb;
}
/**
* 将字符串按指定的分隔符分隔成数组
*
* @param str
* @param separator
* @return
*/
public static String[] split(String str, String separator) {
return StringUtils.split(str, separator);
}
/**
* 获取字符串被特定字符分割后的最后一个元素
*/
public static String getLastSperator(String str, String sperator) {
String[] ss = split(str, sperator);
return ss[ss.length - 1];
}
/**
* 判断传入的字符串是否可以被解析为数字
*/
public static boolean isNumberString(String s) {
try {
if (isEmptyWithTrim(s) || s == null || "null".equals(s)) {
return true;
}
Double.valueOf(s);
return true;
} catch (NumberFormatException e) {
return false;
}
}
public static String cutLastSperator(String s, String sperator) {
return s.substring(0, s.length() - getLastSperator(s, sperator).length() - sperator.length());
}
/**
* 截取字符串中以除某个字符串结尾的所有字符
*/
public static String cutSperatorLast(String s, String sperator) {
if (s == null || s.length() == 0) {
return s;
}
if (s.substring(s.length() - sperator.length()).equals(sperator)) {
return cutSperatorLast(s.substring(0, s.length() - 1), sperator);
}
return s;
}
public static String toUtf8String(String s) {
StringBuffer sb = new StringBuffer();
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if (c >= 0 && c <= 255) {
sb.append(c);
} else {
byte[] b;
try {
b = Character.toString(c).getBytes("utf-8");
} catch (Exception ex) {
ex.printStackTrace();
b = new byte[0];
}
for (int j = 0; j < b.length; j++) {
int k = b[j];
if (k < 0) {
k += 256;
}
sb.append("%" + Integer.toHexString(k).toUpperCase());
}
}
}
return sb.toString();
}
/**
* 根据不同浏览器将文件名中的汉字转为UTF8编码的串,以便下载时能正确显示另存的文件名.
*
* @param s 原文件名
* @return 重新编码后的文件名
*/
public static String toUtf8String(HttpServletRequest request, String s) {
String agent = request.getHeader("User-Agent");
try {
boolean isFireFox = (agent != null && agent.toLowerCase().indexOf("firefox") != -1);
if (isFireFox) {
// s = new String(s.getBytes("UTF-8"), "ISO8859-1");
} else {
s = StringUtil.toUtf8String(s);
if ((agent != null && agent.indexOf("MSIE") != -1)) {
if (s.length() > 150) {
// 根据request的locale 得出可能的编码
s = new String(s.getBytes("UTF-8"), "ISO8859-1");
}
}
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return s;
}
/**
* 把Json字符串中属性值为空字符串的值替换成null
* 例如:"bespeakTakeTime": "", 替换成 "bespeakTakeTime": null,
*
* @param sourceString
* @param properties
* @return
*/
public static String replaceByProperties(String sourceString, String properties) {
String targetString = "";
String[] stringArray = properties.split(",");
for (int i = 0; i < stringArray.length; i++) {
String s = stringArray[i];
String sourceSubStr = "\"" + s + "\":\"\",";
String targetSubStr = "\"" + s + "\": null,";
targetString = sourceString.replace(sourceSubStr, targetSubStr);
}
return targetString;
}
/**
* 把String List转成逗号分隔的String
*
* @param stringList
* @return
*/
public static String listToString(List<String> stringList) {
if (stringList == null) {
return null;
}
StringBuilder result = new StringBuilder();
boolean flag = false;
for (String string : stringList) {
if (flag) {
result.append(",");
} else {
flag = true;
}
result.append(string);
}
return result.toString();
}
/**
* 校验邮箱
*
* @param email
* @return 校验通过返回true,否则返回false
*/
public static boolean isEmail(String email) {
return Pattern.matches(REGEX_EMAIL, email);
}
/**
* 校验邮箱
*
* @param emailMulti
* @return 逗号分隔的多个邮件地址都校验通过返回true,否则返回false
*/
public static boolean isEmailMulti(String emailMulti) {
String[] emailAttr = emailMulti.split(",");
boolean isEmail = true;
for (String email : emailAttr) {
if (!isEmail(email)) {
isEmail = false;
}
}
return isEmail;
}
/**
* List<Long> 转String 用于sql
*
* @param lists
* @return
*/
public static String listLongToStr(List<Long> lists) {
StringBuffer str = new StringBuffer("");
for (Long value : lists) {
str.append(value).append(",");
}
return str.toString().substring(0, str.toString().lastIndexOf(","));
}
/**
* List<Integer> 转String 用于sql
*
* @param lists
* @return
*/
public String listIntegerToStr(List<Integer> lists) {
StringBuffer str = new StringBuffer("");
for (Integer value : lists) {
str.append(value).append(",");
}
return str.toString().substring(0, str.toString().lastIndexOf(","));
}
/**
* List<String> 转String 用于sql
*
* @param lists
* @return
*/
public String listStringToStr(List<String> lists) {
StringBuffer srt = new StringBuffer("");
for (String value : lists) {
srt.append("'").append(value).append("'").append(",");
}
return srt.toString().substring(0, srt.toString().lastIndexOf(","));
}
public static String getRandomString(int length) {
return RandomStringUtils.randomAlphanumeric(length);
}
public static String[] splitForList(String str, String regex) {
ArrayList resultStr = new ArrayList();
if (!isEmptyWithTrim(str)) {
String[] strs = str.split(regex);
for (String s : strs) {
if (!isEmptyWithTrim(s)) {
resultStr.add(s.trim());
}
}
}
return (String[]) resultStr.toArray(new String[resultStr.size()]);
}
/**
* 获取固定字节长度字符串
* @param original
* @param length
* @return
*/
public static String formatStringLength(String original, int length) {
if (original == null) {
original = "";
}
int newLength = original.getBytes().length;
while (newLength > length) {
original = original.substring(0,original.length() - 1);
newLength = original.getBytes().length;
}
return original;
}
/**
* 获取固定字节长度字符串
* @param original
* @param length
* @return
*/
public static String formatStringLength(String original, long length) {
if (original == null) {
original = "";
}
long newLength = original.getBytes().length;
while (newLength > length) {
original = original.substring(0, original.length() - 1);
newLength = original.getBytes().length;
}
return original;
}
public static String autoCompletionStringAfterByZero(String _orig, long _length) {
if (_orig == null) {
_orig = "";
}
while (_orig.length() < _length) {
_orig += "0";
}
return _orig;
}
public static String formatInteger(int num, int length) {
String number = (new Integer(num)).toString();
while (number.length() < length) {
number = "0" + number;
}
return number;
}
/**
* String List 转 String[]
*
* @param strList
* @return
*/
public static String[] listRotationArray(List<String> strList) {
if (strList == null || strList.size() == 0) {
return new String[]{""};
}
return (String[]) strList.toArray(new String[strList.size()]);
}
//如果不足9位置,那么前面补充0
private String strFormat(int length, String strOrign, boolean isAfter, String str) {
if (strOrign == null) {
strOrign = "000000000";
} else {
strOrign = strOrign.trim();
}
while (strOrign.getBytes().length > length) {
strOrign = strOrign.substring(0, strOrign.length() - 1);
}
//不够9为前面补0
while (strOrign.getBytes().length < 9) {
strOrign = "0" + strOrign;
}
//9位之后补空格
while (strOrign.getBytes().length < length) {
strOrign += " ";
}
return strOrign;
}
/**
* 后补空格
*
* @param length
* @param strOrign
* @return
*/
public static String strFormat(int length, String strOrign) {
if (strOrign == null) {
strOrign = " ";
} else {
strOrign = strOrign.trim();
}
while (strOrign.getBytes().length > length) {
strOrign = strOrign.substring(0, strOrign.length() - 1);
}
//9位之后补空格
while (strOrign.getBytes().length < length) {
strOrign += " ";
}
return strOrign;
}
/**
* Java实现按字节长度截取字符串
* @param s
* @param length
* @return
* @throws Exception
*/
public static String bSubstring(String s, int length) throws Exception {
byte[] bytes = s.getBytes("Unicode");
int n = 0; // 表示当前的字节数
int i = 2; // 要截取的字节数,从第3个字节开始
for (; i < bytes.length && n < length; i++) {
// 奇数位置,如3、5、7等,为UCS2编码中两个字节的第二个字节
if (i % 2 == 1) {
n++; // 在UCS2第二个字节时n加1
} else {
// 当UCS2编码的第一个字节不等于0时,该UCS2字符为汉字,一个汉字算两个字节
if (bytes[i] != 0) {
n++;
}
}
}
// 如果i为奇数时,处理成偶数
if (i % 2 == 1) {
// 该UCS2字符是汉字时,去掉这个截一半的汉字
if (bytes[i - 1] != 0) {
i = i - 1;
} else {
// 该UCS2字符是字母或数字,则保留该字符
i = i + 1;
}
}
return new String(bytes, 0, i, "Unicode");
}
/**
* 获取固定字节长度字符串
* @param length
* @param strOrign
* @param strFix
* @return
*/
public static String strLengthFormatNew(int length, String strOrign, String strFix) {
strOrign = strOrign == null ? strFix : strOrign.trim();
StringBuffer resultStrBuffer = null;
if (strOrign.getBytes().length > length) {
strOrign = strOrign.substring(0, length);
while (resultStrBuffer.toString().getBytes().length > length) {
// strOrign = strOrign.substring(0, strOrign.length() - 1);
strOrign = strOrign.substring(0, length);
}
resultStrBuffer = new StringBuffer(strOrign);
} else {
resultStrBuffer = new StringBuffer(strOrign);
//9位之后补空格
while (resultStrBuffer.toString().getBytes().length < length) {
resultStrBuffer.append(strFix);
}
}
return resultStrBuffer.toString();
}
/**
* @param length
* @param strOrign
* @return
*/
@Deprecated
public static String strLengthFormat(int length, String strOrign, String strFix) {
if (strOrign == null) {
strOrign = " ";
} else {
strOrign = strOrign.trim();
}
while (strOrign.getBytes().length > length) {
// strOrign = strOrign.substring(0, strOrign.length() - 1);
strOrign = strOrign.substring(0, length);
}
//9位之后补空格
while (strOrign.getBytes().length < length) {
strOrign += strFix;
}
return strOrign;
}
/**
* 前面补数据
*
* @param length
* @param strOrign
* @return
*/
public static String strFrontFormat(int length, String strOrign, String strFix) {
if (strOrign == null) {
strOrign = " ";
} else {
strOrign = strOrign.trim();
}
while (strOrign.getBytes().length > length) {
// strOrign = strOrign.substring(0, strOrign.length() - 1);
strOrign = strOrign.substring(0, length);
}
//前面补零
while (strOrign.getBytes().length < length) {
strOrign = strFix + strOrign;
}
return strOrign;
}
/**
* 前面补数据
*
* @param length
* @param strOrign
* @return
*/
public static String strFrontFormatNew(int length, String strOrign, String strFix) {
strOrign = strOrign == null ? strFix : strOrign.trim();
if (strOrign == null) {
strOrign = " ";
} else {
strOrign = strOrign.trim();
}
while (strOrign.getBytes().length > length) {
// strOrign = strOrign.substring(0, strOrign.length() - 1);
strOrign = strOrign.substring(0, length);
}
//前面补零
while (strOrign.getBytes().length < length) {
strOrign = strFix + strOrign;
}
return strOrign;
}
/**
* sql in查询条件数量分割
*
* @param orignList
* @param splitCount
* @return
*/
public static List<List> listSplit(List orignList, int splitCount) {
List<List> resultList = new ArrayList<>();
if (orignList == null || orignList.isEmpty()) {
resultList = null;
} else {
int orignListSize = orignList.size();
int cycleTimes = (orignListSize + splitCount - 1) / splitCount;
for (int i = 1; i <= cycleTimes; i++) {
List list = orignList.subList(splitCount * (i - 1), (splitCount * i > orignListSize ? orignListSize : splitCount * i));
resultList.add(list);
}
}
return resultList;
}
public static String valueOf(Object obj) {
return (obj == null || obj.toString().trim().equals("")) ? null : obj.toString();
}
public static String formatStringLaterSpace(String original, long length) {
String result = "";
long bytes = 0;
int index = 0;
while (bytes < length) {
String s = " ";
if (index < original.length()) {
s = original.substring(index, index + 1);
}
bytes += s.getBytes().length;
if (bytes > length) {
for (int i = 0; i < bytes - length; i++) {
result += " ";
}
} else {
result += s;
}
index++;
}
return result;
}
/**
* BigDecimal 格式化
* @param value
* @param wholeLength --整数位
* @param decimalLength --小数位
* @return
*/
public static String formatBigDecimal(BigDecimal value, long wholeLength, long decimalLength){
String str = "" + value;
if(!str.contains(".")){
str = str + ".0" ;
if(str.length() == 2){
str = "0" + str;
}
}
String[]arrays = str.split("\\.");
//整数位格式化
while (arrays[0].length() < wholeLength) {
arrays[0] = "0" + arrays[0];
}
//小数位格式化
while (arrays[1].length() < decimalLength) {
arrays[1] = arrays[1] + "0";
}
return arrays[0] + "." + arrays[1];
}
/**
* BigDecimal 格式化
* @param value
* @param type --小数位 舍入方式
* @param wholeLength --整数位
* @param decimalLength --小数位
* @return
*/
public static String formatBigDecimalNoPoint(BigDecimal value, int type,int wholeLength, int decimalLength){
String str = value.setScale(decimalLength,type).toString();
String[]arrays = str.split("\\.");
//整数位格式化
while (arrays[0].length() < wholeLength) {
arrays[0] = "0" + arrays[0];
}
//小数位格式化
while (arrays[1].length() < decimalLength) {
arrays[1] = arrays[1] + "0";
}
return arrays[0] + arrays[1];
}
/**
* BigDecimal 格式化
* @param value
* @param decimalLength --小数位
* @return
*/
public static String formatBigDecimalNew(BigDecimal value, int decimalLength){
return formatBigDecimalNew(value,BigDecimal.ROUND_HALF_UP,19, decimalLength);
}
/**
* BigDecimal 格式化
* @param value
* @param roundHalfType --小数位 舍入方式
* @param wholeLength --整数位
* @param decimalLength --小数位
* @return
*/
public static String formatBigDecimalNew(BigDecimal value, int roundHalfType,int wholeLength, int decimalLength){
String result = "";
if(value != null) {
String valueNewStr = value.setScale(decimalLength,roundHalfType).toString();
if(valueNewStr.length() > wholeLength){
String[] valueNewArry = valueNewStr.split("\\.");
if(valueNewArry[0].length() > wholeLength){
result = valueNewArry[0].substring(wholeLength - 1) + "." + valueNewArry[1];
} else {
result = valueNewStr;
}
} else {
result = valueNewStr;
}
}
return result;
}
public static void main(String[] args){
BigDecimal aa = new BigDecimal("111.9992440000600000");
String valueNew = StringUtil.formatBigDecimalNew(aa,BigDecimal.ROUND_HALF_UP,3,5);
String valueNew1 = StringUtil.formatBigDecimalNew(null,5);
System.out.println(valueNew);
System.out.println(valueNew1);
}
}