StringUtils.java
28.5 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
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
package com.canrd.webmagic.common.utils;
import com.canrd.webmagic.common.constant.Constant;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.CollectionUtils;
import java.math.BigDecimal;
import java.nio.charset.StandardCharsets;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;
import java.util.stream.Collectors;
import java.util.stream.Stream;
/**
* @param
* @version 0.1.0
* @Description
* @return
* @date 2021/4/16 17:44
* @since 0.1.0
*/
@Slf4j
public class StringUtils {
/**
* 首字母变小写
*
* @param str
* @return
*/
public static String firstCharToLowerCase(String str) {
char firstChar = str.charAt(0);
if (firstChar >= 'A' && firstChar <= 'Z') {
char[] arr = str.toCharArray();
arr[0] += ('a' - 'A');
return new String(arr);
}
return str;
}
/**
* 首字母变大写
*
* @param str
* @return
*/
public static String firstCharToUpperCase(String str) {
char firstChar = str.charAt(0);
if (firstChar >= 'a' && firstChar <= 'z') {
char[] arr = str.toCharArray();
arr[0] -= ('a' - 'A');
return new String(arr);
}
return str;
}
/**
* 判断是否为空
*
* @param str
* @return
*/
public static boolean isEmpty(final String str) {
return (str == null) || (str.length() == 0);
}
/**
* 判断是否不为空
*
* @param str
* @return
*/
public static boolean isNotEmpty(final String str) {
return !isEmpty(str);
}
/**
* 判断是否空白
*
* @param str
* @return
*/
public static boolean isBlank(final String str) {
int strLen;
if ((str == null) || ((strLen = str.length()) == 0)) {
return true;
}
for (int i = 0; i < strLen; i++) {
if (!Character.isWhitespace(str.charAt(i))) {
return false;
}
}
return true;
}
/**
* 判断是否不是空白
*
* @param str
* @return
*/
public static boolean isNotBlank(final String str) {
return !isBlank(str);
}
/**
* 判断多个字符串全部是否为空
*
* @param strings
* @return
*/
public static boolean isAllEmpty(String... strings) {
if (strings == null) {
return true;
}
for (String str : strings) {
if (isNotEmpty(str)) {
return false;
}
}
return true;
}
/**
* 判断多个字符串其中任意一个是否为空
*
* @param strings
* @return
*/
public static boolean isHasEmpty(String... strings) {
if (strings == null) {
return true;
}
for (String str : strings) {
if (isEmpty(str)) {
return true;
}
}
return false;
}
/**
* 判断多个字符串是否都为blank
*
* @param strings
* @return
*/
public static boolean isAllBlank(String... strings) {
if (strings == null) {
return true;
}
for (String str : strings) {
if (isNotBlank(str)) {
return false;
}
}
return true;
}
/**
* checkValue为 null 或者为 "" 时返回 defaultValue
*
* @param checkValue
* @param defaultValue
* @return
*/
public static String isEmpty(String checkValue, String defaultValue) {
return isEmpty(checkValue) ? defaultValue : checkValue;
}
/**
* 字符串不为 null 而且不为 "" 并且等于other
*
* @param str
* @param other
* @return
*/
public static boolean isNotEmptyAndEqualsOther(String str, String other) {
return !isEmpty(str) && str.equals(other);
}
/**
* 字符串不为 null 而且不为 "" 并且不等于other
*
* @param str
* @param other
* @return
*/
public static boolean isNotEmptyAndNotEqualsOther(String str, String... other) {
if (isEmpty(str)) {
return false;
}
for (String s : other) {
if (str.equals(s)) {
return false;
}
}
return true;
}
/**
* 字符串不等于other
*
* @param str
* @param other
* @return
*/
public static boolean isNotEqualsOther(String str, String... other) {
for (String s : other) {
if (s.equals(str)) {
return false;
}
}
return true;
}
/**
* 判断字符串不为空
*
* @param strings
* @return
*/
public static boolean isNotEmpty(String... strings) {
if (strings == null || strings.length == 0) {
return false;
}
for (String str : strings) {
if (str == null || "".equals(str.trim())) {
return false;
}
}
return true;
}
/**
* 比较字符相等
*
* @param value
* @param equals
* @return
*/
public static boolean equals(String value, String equals) {
if (isAllEmpty(value, equals)) {
return true;
}
//进一步判断value是不是空,如果是空,要直接equals会报NPE异常
if (value == null) {
return false;
}
return value.equals(equals);
}
/**
* @description 将字符串转换为数字数组
* @author 黄楷涵
* @date 2020/9/15
*/
public static int[] stringParseToInt(String str, String regex) {
return stringParseToInt(str.split(regex));
}
/**
* @description 将字符串数组转换为数字数组
* @author dengbin
* @date 2020/9/15
*/
public static int[] stringParseToInt(String[] str) {
int[] num = new int[str.length];
for (int i = 0; i < str.length; i++) {
num[i] = Integer.parseInt(str[i]);
}
return num;
}
/**
* 比较字符串不相等
*
* @param value
* @param equals
* @return
*/
public static boolean isNotEquals(String value, String equals) {
return !equals(value, equals);
}
public static String[] split(String content, String separatorChars) {
return splitWorker(content, separatorChars, -1, false);
}
public static String[] split(String str, String separatorChars, int max) {
return splitWorker(str, separatorChars, max, false);
}
public static final String[] EMPTY_STRING_ARRAY = new String[0];
private static String[] splitWorker(String str, String separatorChars, int max, boolean preserveAllTokens) {
if (str == null) {
return null;
}
int len = str.length();
if (len == 0) {
return EMPTY_STRING_ARRAY;
}
List<String> list = new ArrayList<String>();
int sizePlus1 = 1;
int i = 0, start = 0;
boolean match = false;
boolean lastMatch = false;
if (separatorChars == null) {
while (i < len) {
if (Character.isWhitespace(str.charAt(i))) {
if (match || preserveAllTokens) {
lastMatch = true;
if (sizePlus1++ == max) {
i = len;
lastMatch = false;
}
list.add(str.substring(start, i));
match = false;
}
start = ++i;
continue;
}
lastMatch = false;
match = true;
i++;
}
} else if (separatorChars.length() == 1) {
char sep = separatorChars.charAt(0);
while (i < len) {
if (str.charAt(i) == sep) {
if (match || preserveAllTokens) {
lastMatch = true;
if (sizePlus1++ == max) {
i = len;
lastMatch = false;
}
list.add(str.substring(start, i));
match = false;
}
start = ++i;
continue;
}
lastMatch = false;
match = true;
i++;
}
} else {
while (i < len) {
if (separatorChars.indexOf(str.charAt(i)) >= 0) {
if (match || preserveAllTokens) {
lastMatch = true;
if (sizePlus1++ == max) {
i = len;
lastMatch = false;
}
list.add(str.substring(start, i));
match = false;
}
start = ++i;
continue;
}
lastMatch = false;
match = true;
i++;
}
}
if (match || (preserveAllTokens && lastMatch)) {
list.add(str.substring(start, i));
}
return list.toArray(EMPTY_STRING_ARRAY);
}
/**
* 消除转义字符
*
* @param str
* @return
*/
public static String escapeXml(String str) {
if (str == null) {
return "";
}
StringBuilder sb = new StringBuilder();
for (int i = 0; i < str.length(); ++i) {
char c = str.charAt(i);
switch (c) {
case '\u00FF':
case '\u0024':
break;
case '&':
sb.append("&");
break;
case '<':
sb.append("<");
break;
case '>':
sb.append(">");
break;
case '\"':
sb.append(""");
break;
case '\'':
sb.append("'");
break;
default:
if (c <= '\u001F') {
break;
}
if (c >= '\uE000' && c <= '\uF8FF') {
break;
}
if (c >= '\uFFF0') {
break;
}
sb.append(c);
break;
}
}
return sb.toString();
}
/**
* 将字符串中特定模式的字符转换成map中对应的值
*
* @param s 需要转换的字符串
* @param map 转换所需的键值对集合
* @return 转换后的字符串
*/
public static String replace(String s, Map<String, Object> map) {
StringBuilder ret = new StringBuilder((int) (s.length() * 1.5));
int cursor = 0;
for (int start, end; (start = s.indexOf("${", cursor)) != -1 && (end = s.indexOf("}", start)) != -1; ) {
ret.append(s.substring(cursor, start)).append(map.get(s.substring(start + 2, end)));
cursor = end + 1;
}
ret.append(s.substring(cursor, s.length()));
return ret.toString();
}
public static String replace(String s, Object... objs) {
if (objs == null || objs.length == 0) {
return s;
}
if (!s.contains("{}")) {
return s;
}
StringBuilder ret = new StringBuilder((int) (s.length() * 1.5));
int cursor = 0;
int index = 0;
for (int start; (start = s.indexOf("{}", cursor)) != -1; ) {
ret.append(s.substring(cursor, start));
if (index < objs.length) {
ret.append(objs[index]);
} else {
ret.append("{}");
}
cursor = start + 2;
index++;
}
ret.append(s.substring(cursor, s.length()));
return ret.toString();
}
/**
* 转换为字节数组
*
* @param bytes
* @return
*/
public static String toString(byte[] bytes) {
return new String(bytes, StandardCharsets.UTF_8);
}
/**
* 转换为字节数组
*
* @param str
* @return
*/
public static byte[] getBytes(String str) {
return str != null ? str.getBytes(StandardCharsets.UTF_8) : null;
}
public static boolean isNumeric(String cs) {
if (isEmpty(cs)) {
return false;
}
for (int i = 0, sz = cs.length(); i < sz; ++i) {
if (!Character.isDigit(cs.charAt(i))) {
return false;
}
}
return true;
}
/**
* 手机号脱敏
*
* @param phoneNumber
* @return
*/
public static String desensitizedPhoneNumber(String phoneNumber) {
if (StringUtils.isNotEmpty(phoneNumber)) {
phoneNumber = phoneNumber.replaceAll("(\\w{3})\\w*(\\w{4})", "$1****$2");
}
return phoneNumber;
}
/**
* 校验3位小数
*
* @param str
* @return
*/
public static boolean checkDecimal(String str) {
return Pattern.compile(Constant.DICMAL_REGEXP).matcher(str).find();
}
/**
* 校验11位国内手机号
* <p>规则: 11位数,首位必须为1,第二位可以是3-9;其他位数不限制
*
* @param phone 手机号
* @return 格式正确则返回false; 反之为true
* @author A80080
* @createDate 2020/12/19
*/
public static boolean checkPhoneNum(String phone) {
return StringUtils.isEmpty(phone) || !Pattern.compile("^1([3-9])[0-9]{9}$").matcher(phone).find();
}
/**
* @param str
* @return boolean
* @Description 判断字符串是否含有空格
* @version 0.1.0
* @author 邓彬
* @date 2021/1/13 18:10
* @since 0.1.0
*/
public static boolean checkStringContainEmpty(String str) {
return !StringUtils.isEmpty(str) && str.contains(" ");
}
/**
* 字符串切分
*
* @param splitStr
* @param splitFlag
* @return
*/
public static List<String> splitTag(String splitStr, String splitFlag) {
return StringUtils.isBlank(splitStr) || StringUtils.isBlank(splitFlag) ?
new ArrayList<>() :
Arrays.stream(splitStr.split(splitFlag)).collect(Collectors.toList());
}
/**
* @param name
* @return true代表全是汉字
* @Description 校验String是否全是中文
* @version 0.1.0
* @author 邓彬
* @date 2021/1/18 19:54
* @since 0.1.0
*/
public static boolean checkNameChina(String name) {
boolean res = true;
char[] cTemp = name.toCharArray();
for (int i = 0; i < name.length(); i++) {
if (!isChinese(cTemp[i])) {
res = false;
break;
}
}
return res;
}
/**
* @param c
* @return true代表是汉字
* @Description 判定输入的是否是汉字
* @version 0.1.0
* @author 邓彬
* @date 2021/1/18 19:53
* @since 0.1.0
*/
public static boolean isChinese(char c) {
Character.UnicodeBlock ub = Character.UnicodeBlock.of(c);
if (ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS
|| ub == Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS
|| ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A
|| ub == Character.UnicodeBlock.GENERAL_PUNCTUATION
|| ub == Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION
|| ub == Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS) {
return true;
}
return false;
}
/**
* @param c
* @return true代表符合条件
* @Description 校验某个字符是否是a-z、A-Z、_、0-9
* @version 0.1.0
* @author 邓彬
* @date 2021/1/18 19:56
* @since 0.1.0
*/
public static boolean isWord(char c) {
return Pattern.compile("[\\w]").matcher("" + c).matches();
}
/**
* @param str
* @return boolean
* @Description 字符串是否仅包含数字和字母
* @version 0.1.0
* @author 邓彬
* @date 2021/1/18 20:00
* @since 0.1.0
*/
public static boolean isLetterDigit(String str) {
return str.matches("^[a-z0-9A-Z]+$");
}
/**
* @param str
* @return boolean
* @Description 判断是否是纯数字
* @version 0.1.0
* @author 邓彬
* @date 2021/1/23 16:33
* @since 0.1.0
*/
public static boolean isDigit(String str) {
return str.matches("^[0-9]+$");
}
/**
* 是否合法手机号
*
* @param phone
* @return
*/
public static boolean isMobilePhone(String phone) {
return Pattern.compile(Constant.PHONE_REGEXP).matcher(phone).matches();
}
/**
* 是否脱敏手机号
*
* @param phone
* @return
*/
public static boolean isDesensitizationMobilePhone(String phone) {
return Pattern.compile(Constant.PHONE_DESENSITIZATION_REGEXP).matcher(phone).matches();
}
/**
* 判断String是否是整数<br>
* 支持10进制
*
* @param s String
* @return 是否为整数
*/
public static boolean isInteger(String s) {
try {
Integer.parseInt(s);
} catch (NumberFormatException e) {
return false;
}
return true;
}
/**
* 判断对象能否转
*
* @param o Object
* @return 是否为Integer
*/
public static boolean isInteger(Object o) {
try {
Integer.parseInt(String.valueOf(o));
return true;
} catch (NumberFormatException e) {
return false;
}
}
public static String escapeCharacter(String str) {
if (!isEmpty(str)) {
if (str.contains("\\")) {
str = str.replaceAll("\\\\", "\\\\\\\\");
}
}
return str;
}
/**
* @param str
* @return boolean
* @Description 匹配字符串是,数字、26个英文字母、下划线组成的8-16位的字符串组合
* @version 0.1.0
* @author A80077-刘始达
* @date 2021/04/06
* @since 0.1.0
*/
public static boolean checkPWD(String str) {
return str.matches("^(?=.*([a-zA-Z].*))(?=.*[0-9].*)[a-zA-Z0-9-_]{8,16}+$");
}
/**
* 判断是否包含特殊字符
*
* @param str
* @return
*/
public static boolean checkSpecificSymbol(String str) {
String regEx = "[ _`~!@#$%^&*()+=|{}':;',\\[\\].<>/?~!@#¥%……&*()——+|{}【】‘;:”“’。,、?]|\n|\r|\t";
Pattern p = Pattern.compile(regEx);
Matcher m = p.matcher(str);
return m.find();
}
/**
* 拼接图片标签url
*
* @param str
* @param ossDomain
* @return
*/
public static String replaceHtmlTag(String str, String ossDomain) {
if (StringUtils.isEmpty(str)) {
return "";
}
if (StringUtils.isEmpty(ossDomain)) {
return str;
}
return replaceHtmlTag(str, "img", "src", "src=\"" + ossDomain, "\"");
}
/**
* html格式处理:替换指定标签的属性和值
*
* @param str 需要处理的字符串
* @param tag 标签名称
* @param tagAttrib 要替换的标签属性值
* @param startTag 新标签开始标记
* @param endTag 新标签结束标记
* @return
*/
public static String replaceHtmlTag(String str, String tag, String tagAttrib, String startTag, String endTag) {
String regxpForTag = "<\\s*" + tag + "\\s+([^>]*)\\s*";
String regxpForTagAttrib = tagAttrib + "=\\s*\"([^\"]+)\"";
Pattern patternForTag = Pattern.compile(regxpForTag, Pattern.CASE_INSENSITIVE);
Pattern patternForAttrib = Pattern.compile(regxpForTagAttrib, Pattern.CASE_INSENSITIVE);
Matcher matcherForTag = patternForTag.matcher(str);
StringBuffer sb = new StringBuffer();
boolean result = matcherForTag.find();
while (result) {
StringBuffer stringBuffer = new StringBuffer("<" + tag + " ");
Matcher matcherForAttrib = patternForAttrib.matcher(matcherForTag.group(1));
if (matcherForAttrib.find()) {
String attributeStr = matcherForAttrib.group(1);
if (!attributeStr.contains("https") && !attributeStr.contains("http")) {
matcherForAttrib.appendReplacement(stringBuffer, startTag + attributeStr + endTag);
}
}
matcherForAttrib.appendTail(stringBuffer);
matcherForTag.appendReplacement(sb, stringBuffer.toString());
result = matcherForTag.find();
}
matcherForTag.appendTail(sb);
return sb.toString();
}
/**
* @param str
* @return java.util.Map<String, List < String>>
* @Description 字符串的连续切割 返回中文词组 数字和字母的集合
* @version 0.1.0
* @author 邓彬
* @date 2021/4/15 13:53
* @since 0.1.0
*/
public static Map<String, List<String>> convertStrToChineseList(String str) {
if (StringUtils.isEmpty(str)) {
return null;
}
Map<String, List<String>> resultMap = new HashMap<>();
List<String> chineseList;
List<String> charList;
char[] list = str.toCharArray();
StringBuilder chineseStr = new StringBuilder();
StringBuilder charStr = new StringBuilder();
for (char c : list) {
if (StringUtils.isChinese(c)) {
chineseStr.append(c);
charStr.append(Constant.SLASH_MARK_CHARACTER);
} else if (Character.isLetterOrDigit(c)) {
charStr.append(c);
chineseStr.append(Constant.SLASH_MARK_CHARACTER);
} else {
charStr.append(Constant.SLASH_MARK_CHARACTER);
chineseStr.append(Constant.SLASH_MARK_CHARACTER);
}
}
chineseList = Arrays.stream(chineseStr.toString()
.split(Constant.SLASH_MARK_CHARACTER))
.filter(StringUtils::isNotEmpty).collect(Collectors.toList());
charList = Arrays.stream(charStr.toString()
.split(Constant.SLASH_MARK_CHARACTER))
.filter(StringUtils::isNotEmpty).map(String::toLowerCase).collect(Collectors.toList());
resultMap.put("chineseKey", chineseList);
resultMap.put("charKey", charList);
return resultMap;
}
/**
* 单个字符转换为小写
*
* @param c
* @return
*/
public static char singleCharToLowerCase(char c) {
if (c >= 'A' && c <= 'Z') {
c += ('a' - 'A');
return c;
}
return c;
}
public static boolean isLong(String shopId) {
try {
Long.parseLong(shopId);
} catch (NumberFormatException e) {
return false;
}
return true;
}
public static String priceFormatting(String price) {
if (StringUtils.isEmpty(price)) {
return Constant.EMPTY_STRING;
}
String newPrice = "";
// 价格不存在小数点,则直接返回,无需处理
if (!price.contains(Constant.POINT_BAR_CHARACTER)) {
newPrice = price;
return newPrice;
}
// 有小数点,则分割字符串
String[] s = price.split("\\.");
// 小数位数大于0 且小于2 且存在非0字符
if (s[1].length() > 0 && s[1].length() <= 2 && !judgeIsNumeric(s[1])) {
BigDecimal bigDecimal = new BigDecimal(price).setScale(2, BigDecimal.ROUND_HALF_UP);
newPrice = bigDecimal.toPlainString();
}
// 【保留这个else if】小数位数大于0 且大于2 且存在非0字符,理论上不会存在,因为新增商品时已经限定最多输入2个小数点
else if (s[1].length() > 0 && s[1].length() > 2 && !judgeIsNumeric(s[1])) {
BigDecimal bigDecimal = new BigDecimal(price).setScale(2, BigDecimal.ROUND_HALF_UP);
String[] split = bigDecimal.toPlainString().split("\\.");
if (split[1].length() > 0 && judgeIsNumeric(split[1])) {
newPrice = new BigDecimal(bigDecimal.toPlainString()).setScale(0, BigDecimal.ROUND_HALF_UP).toPlainString();
} else {
newPrice = bigDecimal.toPlainString();
}
} else {
// 小数位数大于0 且小于2 且小数点都是0
newPrice = new BigDecimal(price).setScale(0, BigDecimal.ROUND_HALF_UP).toPlainString();
}
return newPrice;
}
/**
* 判断字符串是否只包含0
*
* @param str
* @return
*/
private static boolean judgeIsNumeric(String str) {
return Pattern.compile("[0]*").matcher(str).matches();
}
/**
* 字符串以","分隔后转为String[]
*
* @param s
* @return
*/
public static String[] string2ArraySplitByComma(String s) {
if (s == null || s.length() == 0) {
return new String[]{};
}
return s.split(Constant.COMMA_CHARACTER);
}
public static boolean isStringArrayContainsSpecifiedValue(String[] strings, String value) {
if (null == strings || strings.length <= 0 || value == null || value.length() == 0) {
return false;
}
boolean isContainsSpecifiedValue = false;
for (String s : strings) {
if (s.equals(value)) {
isContainsSpecifiedValue = true;
break;
}
}
return isContainsSpecifiedValue;
}
/**
* 字符串以","分隔后转为list
*
* @param s
* @return
*/
public static List<String> string2ListSplitByComma(String s) {
List<String> stringList = new ArrayList<>();
if (s == null || s.length() == 0) {
return stringList;
}
String[] strArray = string2ArraySplitByComma(s);
for (String st : strArray) {
if (null != st && st.trim() != null || st.trim().length() != 0) {
stringList.add(st.trim());
}
}
return stringList;
}
/**
* 字符串按照特殊符号
* ".*[`~!@#$%^&*()+=|{}':;',\\[\\].<>/?~!@#¥%……&*()——+|{}【】‘;:”“’。,、?\\\\]+.*"
* 截取转成字符串集合
*
* @param str
* @return
*/
public static List<String> splitStringBySpecificSymbol(String str) throws PatternSyntaxException {
String regEx = "[`~!@#$%^&*()\\-+=|{}':;,\\[\\].<>/?!¥…()—【】‘;:”“’。,、?\\\\]";
Pattern p = Pattern.compile(regEx);
Matcher m = p.matcher(str);
return Stream.of(m.replaceAll("_").split("_")).map(String::trim).filter(StringUtils::isNotBlank).collect(Collectors.toList());
}
/**
* 将list转为字符串,用逗号隔开
*
* @param list
* @return
*/
public static String listTransToStr(List<String> list) {
if (CollectionUtils.isEmpty(list)) return null;
StringBuilder sb = new StringBuilder();
list.forEach(x -> {
sb.append(x);
sb.append(",");
});
String str = sb.toString();
return str.substring(0, str.length() - 1);
}
/**
* 是否包含中英文
*
* @param str
* @return
*/
public static boolean checkCnAndEn(String str) {
String regEx = Constant.CHI_EN_REGEXP;
Pattern p = Pattern.compile(regEx);
Matcher m = p.matcher(str);
return m.find();
}
/**
* @param code 要隐藏显示的字符串
* @param head 前面保留的位数
* @param tail 后面保留的位数
* @return 处理后的字符串
*/
public static String getEncryptCode(String code, int head, int tail) {
int body = code.length() - head - tail;
String regexVar = "(\\w{%d})(\\w{%d})(\\w{%d})";
String regex = String.format(regexVar, head, body, tail);
String bodyPart = code.replaceAll(regex, "$2");
String bodyEncrypt = bodyPart.replaceAll("\\w", "*");
String replacement = String.format("$1%s$3", bodyEncrypt);
return code.replaceAll(regex, replacement);
}
}