-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
StringUtil.java
executable file
·971 lines (869 loc) · 24.7 KB
/
StringUtil.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
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
/*Copyright (C) 2020 THL A29 Limited, a Tencent company. All rights reserved.
This source code is licensed under the Apache License Version 2.0.*/
package apijson;
import java.io.File;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.Objects;
import java.util.regex.Pattern;
/**通用字符串(String)相关类,为null时返回""
* @author Lemon
* @use StringUtil.
*/
public class StringUtil {
private static final String TAG = "StringUtil";
public StringUtil() {
}
public static final String UTF_8 = "utf-8";
public static final String EMPTY = "无";
public static final String UNKNOWN = "未知";
public static final String UNLIMITED = "不限";
public static final String I = "我";
public static final String YOU = "你";
public static final String HE = "他";
public static final String SHE = "她";
public static final String IT = "它";
public static final String MALE = "男";
public static final String FEMALE = "女";
public static final String TODO = "未完成";
public static final String DONE = "已完成";
public static final String FAIL = "失败";
public static final String SUCCESS = "成功";
public static final String SUNDAY = "日";
public static final String MONDAY = "一";
public static final String TUESDAY = "二";
public static final String WEDNESDAY = "三";
public static final String THURSDAY = "四";
public static final String FRIDAY = "五";
public static final String SATURDAY = "六";
public static final String YUAN = "元";
private static String currentString = "";
/**获取刚传入处理后的string
* @must 上个影响currentString的方法 和 这个方法都应该在同一线程中,否则返回值可能不对
* @return
*/
public static String getCurrentString() {
return currentString == null ? "" : currentString;
}
//获取string,为null时返回"" <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
/**获取string,为null则返回""
* @param object
* @return
*/
public static String getString(Object object) {
return object == null ? "" : object.toString();
}
/**获取string,为null则返回""
* @param cs
* @return
*/
public static String getString(CharSequence cs) {
return cs == null ? "" : cs.toString();
}
/**获取string,为null则返回""
* @param s
* @return
*/
public static String getString(String s) {
return s == null ? "" : s;
}
/**获取string,为null则返回""
* ignoreEmptyItem = false;
* split = ","
* @param array
* @return {@link #getString(Object[], boolean)}
*/
public static String getString(Object[] array) {
return getString(array, false);
}
/**获取string,为null则返回""
* split = ","
* @param array
* @param ignoreEmptyItem
* @return {@link #getString(Object[], boolean)}
*/
public static String getString(Object[] array, boolean ignoreEmptyItem) {
return getString(array, null, ignoreEmptyItem);
}
/**获取string,为null则返回""
* ignoreEmptyItem = false;
* @param array
* @param split
* @return {@link #getString(Object[], String, boolean)}
*/
public static String getString(Object[] array, String split) {
return getString(array, split, false);
}
//CS304 Issue link: https://github.com/Tencent/APIJSON/issues/182
/**获取string,为null则返回""
* @param array -the str array given
* @param split -the token used to split
* @param ignoreEmptyItem -whether to ignore empty item or not
* @return {@link #getString(Object[], String, boolean)}
* <p>Here we replace the simple "+" way of concatenating with Stringbuilder 's append</p>
*/
public static String getString(Object[] array, String split, boolean ignoreEmptyItem) {
StringBuilder s = new StringBuilder("");
if (array != null) {
if (split == null) {
split = ",";
}
for (int i = 0; i < array.length; i++) {
if (ignoreEmptyItem && isEmpty(array[i], true)) {
continue;
}
s.append(((i > 0 ? split : "") + array[i]));
}
}
return getString(s.toString());
}
//获取string,为null时返回"" >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
//获取去掉前后空格后的string<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
/**获取去掉前后空格后的string,为null则返回""
* @param object
* @return
*/
public static String getTrimedString(Object object) {
return getTrimedString(getString(object));
}
/**获取去掉前后空格后的string,为null则返回""
* @param cs
* @return
*/
public static String getTrimedString(CharSequence cs) {
return getTrimedString(getString(cs));
}
/**获取去掉前后空格后的string,为null则返回""
* @param s
* @return
*/
public static String getTrimedString(String s) {
return getString(s).trim();
}
//获取去掉前后空格后的string>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
//获取去掉所有空格后的string <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
/**获取去掉所有空格后的string,为null则返回""
* @param object
* @return
*/
public static String getNoBlankString(Object object) {
return getNoBlankString(getString(object));
}
/**获取去掉所有空格后的string,为null则返回""
* @param cs
* @return
*/
public static String getNoBlankString(CharSequence cs) {
return getNoBlankString(getString(cs));
}
/**获取去掉所有空格后的string,为null则返回""
* @param s
* @return
*/
public static String getNoBlankString(String s) {
return getString(s).replaceAll("\\s", "");
}
//获取去掉所有空格后的string >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
//获取string的长度<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
/**获取string的长度,为null则返回0
* @param object
* @param trim
* @return
*/
public static int getLength(Object object, boolean trim) {
return getLength(getString(object), trim);
}
/**获取string的长度,为null则返回0
* @param cs
* @param trim
* @return
*/
public static int getLength(CharSequence cs, boolean trim) {
return getLength(getString(cs), trim);
}
/**获取string的长度,为null则返回0
* @param s
* @param trim
* @return
*/
public static int getLength(String s, boolean trim) {
s = trim ? getTrimedString(s) : s;
return getString(s).length();
}
//获取string的长度>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
//判断字符是否为空 <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
/**判断字符是否为空 trim = true
* @param obj
* @return
*/
public static boolean isEmpty(Object obj) {
return isEmpty(obj, true);
}
/**判断字符是否为空
* @param obj
* @param trim
* @return
*/
public static boolean isEmpty(Object obj, boolean trim) {
return isEmpty(getString(obj), trim);
}
/**判断字符是否为空 trim = true
* @param cs
* @return
*/
public static boolean isEmpty(CharSequence cs) {
return isEmpty(cs, true);
}
/**判断字符是否为空
* @param cs
* @param trim
* @return
*/
public static boolean isEmpty(CharSequence cs, boolean trim) {
return isEmpty(getString(cs), trim);
}
/**判断字符是否为空 trim = true
* @param s
* @return
*/
public static boolean isEmpty(String s) {
return isEmpty(s, true);
}
/**判断字符是否为空
* @param s
* @param trim
* @return
*/
public static boolean isEmpty(String s, boolean trim) {
// Log.i(TAG, "getTrimedString s = " + s);
if (s == null) {
return true;
}
if (trim) {
s = s.trim();
}
if (s.isEmpty()) {
return true;
}
currentString = s;
return false;
}
//判断字符是否为空 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
//判断字符是否非空 <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
/**判断字符是否非空 trim = true
* @param object
* @return
*/
public static boolean isNotEmpty(Object obj) {
return ! isEmpty(obj);
}
/**判断字符是否非空
* @param obj
* @param trim
* @return
*/
public static boolean isNotEmpty(Object obj, boolean trim) {
return ! isEmpty(obj, trim);
}
/**判断字符是否非空 trim = true
* @param cs
* @return
*/
public static boolean isNotEmpty(CharSequence cs) {
return ! isEmpty(cs);
}
/**判断字符是否非空
* @param cs
* @param trim
* @return
*/
public static boolean isNotEmpty(CharSequence cs, boolean trim) {
return ! isEmpty(cs, trim);
}
/**判断字符是否非空 trim = true
* @param s
* @return
*/
public static boolean isNotEmpty(String s) {
return ! isEmpty(s);
}
/**判断字符是否非空
* @param s
* @param trim
* @return
*/
public static boolean isNotEmpty(String s, boolean trim) {
return ! isEmpty(s, trim);
}
//判断字符是否非空 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
//判断字符类型 <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
public static final Pattern PATTERN_NUMBER;
public static final Pattern PATTERN_PHONE;
public static final Pattern PATTERN_EMAIL;
public static final Pattern PATTERN_ID_CARD;
public static final Pattern PATTERN_ALPHA;
public static final Pattern PATTERN_PASSWORD; //TODO
public static final Pattern PATTERN_NAME;
public static final Pattern PATTERN_ALPHA_BIG;
public static final Pattern PATTERN_ALPHA_SMALL;
public static final Pattern PATTERN_BRANCH_URL;
static {
PATTERN_NUMBER = Pattern.compile("^[0-9]+$");
PATTERN_ALPHA = Pattern.compile("^[a-zA-Z]+$");
PATTERN_ALPHA_BIG = Pattern.compile("^[A-Z]+$");
PATTERN_ALPHA_SMALL = Pattern.compile("^[a-z]+$");
PATTERN_NAME = Pattern.compile("^[0-9a-zA-Z_.:]+$");//已用55个中英字符测试通过
//newest phone regex expression reference https://github.com/VincentSit/ChinaMobilePhoneNumberRegex
PATTERN_PHONE = Pattern.compile("^1(?:3\\d{3}|5[^4\\D]\\d{2}|8\\d{3}|7(?:[0-35-9]\\d{2}|4(?:0\\d|1[0-2]|9\\d))|9[0-35-9]\\d{2}|6[2567]\\d{2}|4(?:(?:10|4[01])\\d{3}|[68]\\d{4}|[579]\\d{2}))\\d{6}$");
PATTERN_EMAIL = Pattern.compile("^([a-zA-Z0-9_\\-\\.]+)@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.)|(([a-zA-Z0-9\\-]+\\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\\]?)$");
PATTERN_ID_CARD = Pattern.compile("(^[1-9]\\d{5}(18|19|([23]\\d))\\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\\d{3}[0-9Xx]$)|(^[1-9]\\d{5}\\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\\d{2}$)");
PATTERN_PASSWORD = Pattern.compile("^[0-9a-zA-Z]+$");
PATTERN_BRANCH_URL = Pattern.compile("^[0-9a-zA-Z-_/]+$");
}
/**判断手机格式是否正确
* @param phone
* @return
*/
public static boolean isPhone(String phone) {
if (isNotEmpty(phone, true) == false) {
return false;
}
currentString = phone;
return PATTERN_PHONE.matcher(phone).matches();
}
/**判断手机格式是否正确
* @param s
* @return
*/
public static boolean isPassword(String s) {
return getLength(s, false) >= 6 && PATTERN_PASSWORD.matcher(s).matches();
}
/**判断是否全是数字密码
* @param s
* @return
*/
public static boolean isNumberPassword(String s) {
return getLength(s, false) == 6 && isNumer(s);
}
/**判断email格式是否正确
* @param email
* @return
*/
public static boolean isEmail(String email) {
if (isEmpty(email, true)) {
return false;
}
currentString = email;
return PATTERN_EMAIL.matcher(email).matches();
}
/**判断是否全是验证码
* @param s
* @return
*/
public static boolean isVerify(String s) {
return getLength(s, false) >= 4 && isNumer(s);
}
/**判断是否全是数字
* @param s
* @return
*/
public static boolean isNumer(String s) {
if (isNotEmpty(s, true) == false) {
return false;
}
currentString = s;
return PATTERN_NUMBER.matcher(s).matches();
}
/**判断是否全是字母
* @param s
* @return
*/
public static boolean isAlpha(String s) {
if (isEmpty(s, true)) {
return false;
}
currentString = s;
return PATTERN_ALPHA.matcher(s).matches();
}
/**判断是否全是数字或字母
* @param s
* @return
*/
public static boolean isNumberOrAlpha(String s) {
return isNumer(s) || isAlpha(s);
}
/**判断是否为代码名称,只能包含字母,数字或下划线
* @param s
* @return
*/
public static boolean isName(String s) {
if (s == null || s.isEmpty()) {
return false;
}
String first = s.substring(0, 1);
if ("_".equals(first) == false && PATTERN_ALPHA.matcher(first).matches() == false) {
return false;
}
return s.length() <= 1 ? true : PATTERN_NAME.matcher(s.substring(1)).matches();
}
/**判断是否为首字母大写的代码名称
* @param s
* @return
*/
public static boolean isBigName(String s) {
if (s == null || s.isEmpty() || PATTERN_ALPHA_BIG.matcher(s.substring(0, 1)).matches() == false) {
return false;
}
return s.length() <= 1 ? true : PATTERN_NAME.matcher(s.substring(1)).matches();
}
/**判断是否为首字母小写的代码名称
* @param s
* @return
*/
public static boolean isSmallName(String s) {
if (s == null || s.isEmpty() || PATTERN_ALPHA_SMALL.matcher(s.substring(0, 1)).matches() == false) {
return false;
}
return s.length() <= 1 ? true : PATTERN_NAME.matcher(s.substring(1)).matches();
}
/**判断字符类型是否是身份证号
* @param number
* @return
*/
public static boolean isIDCard(String number) {
if (isNumberOrAlpha(number) == false) {
return false;
}
number = getString(number);
if (number.length() == 15) {
Log.i(TAG, "isIDCard number.length() == 15 old IDCard");
currentString = number;
return true;
}
if (number.length() == 18) {
currentString = number;
return true;
}
return false;
}
public static final String HTTP = "http";
public static final String URL_PREFIX = "http://";
public static final String URL_PREFIXs = "https://";
public static final String URL_STAFFIX = URL_PREFIX;
public static final String URL_STAFFIXs = URL_PREFIXs;
/**判断字符类型是否是网址
* @param url
* @return
*/
public static boolean isUrl(String url) {
if (isNotEmpty(url, true) == false) {
return false;
}
if (! url.startsWith(URL_PREFIX) && ! url.startsWith(URL_PREFIXs)) {
return false;
}
currentString = url;
return true;
}
public static boolean isBranchUrl(String branchUrl) {
if (isEmpty(branchUrl, false)) {
return false;
}
return PATTERN_BRANCH_URL.matcher(branchUrl).matches();
}
public static final String FILE_PATH_PREFIX = "file://";
/**判断文件路径是否存在
* @param path
* @return
*/
public static boolean isFilePathExist(String path) {
return StringUtil.isFilePath(path) && new File(path).exists();
}
public static final String SEPARATOR = "/";
/**判断是否为路径
* @param path
* @return
*/
public static boolean isPath(String path) {
return StringUtil.isNotEmpty(path, true) && path.contains(SEPARATOR)
&& path.contains(SEPARATOR + SEPARATOR) == false && path.endsWith(SEPARATOR) == false;
}
/**判断字符类型是否是路径
* @param path
* @return
*/
public static boolean isFilePath(String path) {
if (isNotEmpty(path, true) == false) {
return false;
}
if (! path.contains(".") || path.endsWith(".")) {
return false;
}
currentString = path;
return true;
}
//判断字符类型 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
//提取特殊字符<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
/**去掉string内所有非数字类型字符
* @param object
* @return
*/
public static String getNumber(Object object) {
return getNumber(getString(object));
}
/**去掉string内所有非数字类型字符
* @param cs
* @return
*/
public static String getNumber(CharSequence cs) {
return getNumber(getString(cs));
}
/**去掉string内所有非数字类型字符
* @param s
* @return
*/
public static String getNumber(String s) {
return getNumber(s, false);
}
//CS304 Issue link: https://github.com/Tencent/APIJSON/issues/182
/**去掉string内所有非数字类型字符
* @param s -string passed in
* @param onlyStart 中间有非数字时只获取前面的数字
* @return limit String
* <p>Here we replace the simple "+" way of concatenating with Stringbuilder 's append</p>
*/
public static String getNumber(String s, boolean onlyStart) {
if (isNotEmpty(s, true) == false) {
return "";
}
StringBuilder numberString = new StringBuilder("");
String single;
for (int i = 0; i < s.length(); i++) {
single = s.substring(i, i + 1);
if (isNumer(single)) {
numberString.append(single);
} else {
if (onlyStart) {
return numberString.toString();
}
}
}
return numberString.toString();
}
//提取特殊字符>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
//校正(自动补全等)字符串<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
/**获取网址,自动补全
* @param url
* @return
*/
public static String getCorrectUrl(String url) {
Log.i(TAG, "getCorrectUrl : \n" + url);
if (isNotEmpty(url, true) == false) {
return "";
}
// if (! url.endsWith("/") && ! url.endsWith(".html")) {
// url = url + "/";
// }
if (isUrl(url) == false) {
return URL_PREFIX + url;
}
return url;
}
/**获取去掉所有 空格 、"-" 、"+86" 后的phone
* @param phone
* @return
*/
public static String getCorrectPhone(String phone) {
if (isNotEmpty(phone, true) == false) {
return "";
}
phone = getNoBlankString(phone);
phone = phone.replaceAll("-", "");
if (phone.startsWith("+86")) {
phone = phone.substring(3);
}
return phone;
}
/**获取邮箱,自动补全
* @param email
* @return
*/
public static String getCorrectEmail(String email) {
if (isNotEmpty(email, true) == false) {
return "";
}
email = getNoBlankString(email);
if (isEmail(email) == false && ! email.endsWith(".com")) {
email += ".com";
}
return email;
}
public static final int PRICE_FORMAT_DEFAULT = 0;
public static final int PRICE_FORMAT_PREFIX = 1;
public static final int PRICE_FORMAT_SUFFIX = 2;
public static final int PRICE_FORMAT_PREFIX_WITH_BLANK = 3;
public static final int PRICE_FORMAT_SUFFIX_WITH_BLANK = 4;
public static final String[] PRICE_FORMATS = {
"", "¥", "元", "¥ ", " 元"
};
/**获取价格,保留两位小数
* @param price
* @return
*/
public static String getPrice(String price) {
return getPrice(price, PRICE_FORMAT_DEFAULT);
}
//CS304 Issue link: https://github.com/Tencent/APIJSON/issues/182
/**获取价格,保留两位小数
* @param price -price passed in
* @param formatType 添加单位(元)
* @return limit String
* <p>Here we replace the simple "+" way of concatenating with Stringbuilder 's append</p>
*/
public static String getPrice(String price, int formatType) {
if (isNotEmpty(price, true) == false) {
return getPrice(0, formatType);
}
//单独写到getCorrectPrice? <<<<<<<<<<<<<<<<<<<<<<
String correctPrice;
StringBuilder correctPriceBuilder = new StringBuilder("");
String s;
for (int i = 0; i < price.length(); i++) {
s = price.substring(i, i + 1);
if (".".equals(s) || isNumer(s)) {
correctPriceBuilder.append(s);
}
}
correctPrice = correctPriceBuilder.toString();
//单独写到getCorrectPrice? >>>>>>>>>>>>>>>>>>>>>>
Log.i(TAG, "getPrice <<<<<<<<<<<<<<<<<< correctPrice = " + correctPrice);
if (correctPrice.contains(".")) {
// if (correctPrice.startsWith(".")) {
// correctPrice = 0 + correctPrice;
// }
if (correctPrice.endsWith(".")) {
correctPrice = correctPrice.replaceAll(".", "");
}
}
Log.i(TAG, "getPrice correctPrice = " + correctPrice + " >>>>>>>>>>>>>>>>");
return isNotEmpty(correctPrice, true) ? getPrice(new BigDecimal(0 + correctPrice), formatType) : getPrice(0, formatType);
}
/**获取价格,保留两位小数
* @param price
* @return
*/
public static String getPrice(BigDecimal price) {
return getPrice(price, PRICE_FORMAT_DEFAULT);
}
/**获取价格,保留两位小数
* @param price
* @return
*/
public static String getPrice(double price) {
return getPrice(price, PRICE_FORMAT_DEFAULT);
}
/**获取价格,保留两位小数
* @param price
* @param formatType 添加单位(元)
* @return
*/
public static String getPrice(BigDecimal price, int formatType) {
return getPrice(price == null ? 0 : price.doubleValue(), formatType);
}
/**获取价格,保留两位小数
* @param price
* @param formatType 添加单位(元)
* @return
*/
public static String getPrice(double price, int formatType) {
String s = new DecimalFormat("#########0.00").format(price);
switch (formatType) {
case PRICE_FORMAT_PREFIX:
return PRICE_FORMATS[PRICE_FORMAT_PREFIX] + s;
case PRICE_FORMAT_SUFFIX:
return s + PRICE_FORMATS[PRICE_FORMAT_SUFFIX];
case PRICE_FORMAT_PREFIX_WITH_BLANK:
return PRICE_FORMATS[PRICE_FORMAT_PREFIX_WITH_BLANK] + s;
case PRICE_FORMAT_SUFFIX_WITH_BLANK:
return s + PRICE_FORMATS[PRICE_FORMAT_SUFFIX_WITH_BLANK];
default:
return s;
}
}
public static String join(String[] arr) {
return join(arr);
}
/** 数组以指定分隔s拼接
* @param arr
* @param s
* @return
*/
public static String join(String[] arr, String s) {
if (s == null) {
s = ",";
}
StringBuilder sb = new StringBuilder();
for (int i = 0; i < arr.length; i++) {
sb.append(arr[i]);
if (i < arr.length-1) {
sb.append(s);
}
}
return sb.toString();
}
/**分割路径
* @param path
* @return
*/
public static String[] splitPath(String path) {
if (StringUtil.isNotEmpty(path, true) == false) {
return null;
}
return isPath(path) ? split(path, SEPARATOR) : new String[] {path};
}
/**将s分割成String[]
* @param s
* @return
*/
public static String[] split(String s) {
return split(s, null);
}
/**将s用split分割成String[]
* trim = true;
* @param s
* @param split
* @return
*/
public static String[] split(String s, String split) {
return split(s, split, true);
}
/**将s用split分割成String[]
* @param s
* @param trim 去掉前后两端的split
* @return
*/
public static String[] split(String s, boolean trim) {
return split(s, null, trim);
}
/**将s用split分割成String[]
* @param s
* @param split
* @param trim 去掉前后两端的split
* @return
*/
public static String[] split(String s, String split, boolean trim) {
s = getString(s);
if (s.isEmpty()) {
return null;
}
if (isEmpty(split, false)) {
split = ",";
}
if (trim) {
while (s.startsWith(split)) {
s = s.substring(split.length());
}
while (s.endsWith(split)) {
s = s.substring(0, s.length() - split.length());
}
}
return s.contains(split) ? s.split(split) : new String[]{s};
}
/**
* @param key
* @param suffix
* @return key + suffix,第一个字母小写
*/
public static String addSuffix(String key, String suffix) {
key = getNoBlankString(key);
if (key.isEmpty()) {
return firstCase(suffix);
}
return firstCase(key) + firstCase(suffix, true);
}
/**
* @param key
*/
public static String firstCase(String key) {
return firstCase(key, false);
}
/**
* @param key
* @param upper
* @return
*/
public static String firstCase(String key, boolean upper) {
key = getString(key);
if (key.isEmpty()) {
return "";
}
String first = key.substring(0, 1);
key = (upper ? first.toUpperCase() : first.toLowerCase()) + key.substring(1, key.length());
return key;
}
/**全部大写
* @param s
* @return
*/
public static String toUpperCase(String s) {
return toUpperCase(s, false);
}
/**全部大写
* @param s
* @param trim
* @return
*/
public static String toUpperCase(String s, boolean trim) {
s = trim ? getTrimedString(s) : getString(s);
return s.toUpperCase();
}
/**全部小写
* @param s
* @return
*/
public static String toLowerCase(String s) {
return toLowerCase(s, false);
}
/**全部小写
* @param s
* @return
*/
public static String toLowerCase(String s, boolean trim) {
s = trim ? getTrimedString(s) : getString(s);
return s.toLowerCase();
}
public static String concat(String left, String right) {
return concat(left, right, null);
}
public static String concat(String left, String right, String split) {
return concat(left, right, split, true);
}
public static String concat(String left, String right, boolean trim) {
return concat(left, right, null, trim);
}
public static String concat(String left, String right, String split, boolean trim) {
if (isEmpty(left, trim)) {
return right;
}
if (isEmpty(right, trim)) {
return left;
}
if (split == null) {
split = ",";
}
return left + split + right;
}
//校正(自动补全等)字符串>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
public static boolean equals(Object s1, Object s2) {
return Objects.equals(s1, s2);
}
public static boolean equalsIgnoreCase(String s1, String s2) {
if (s1 == s2) {
return true;
}
if (s1 == null || s2 == null) {
return false;
}
return s1.equalsIgnoreCase(s2);
}
}