rk
昨天 4a8ff39b0fab0627ef8f7459587d514cc01c3676
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
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
package com.doumee.lib_coremodel.util;
 
import android.text.TextUtils;
 
import org.json.JSONException;
import org.json.JSONObject;
 
import java.security.NoSuchAlgorithmException;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Random;
import java.util.Set;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
 
import javax.crypto.Cipher;
import javax.crypto.Mac;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
 
 
/**
 * 字符串操作工具包
 */
public class StringUtil {
    public final static String EMPTY="";
    private final static Pattern emailer = Pattern.compile("\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*");
    private final static Pattern identityCard = Pattern.compile("(\\d{14}[0-9xX])|(\\d{17}[0-9xX])");
    private final static Pattern passer = Pattern.compile("[A-Za-z0-9@._-]{6,20}");
    private final static Pattern pureNumber = Pattern.compile("[0-9]{8,30}");
    private final static Pattern phoner = Pattern.compile("1[0-9]{10}");
    private final static Pattern bankCarder = Pattern.compile("[0-9]{16}|[0-9]{19}");
    private final static Pattern user = Pattern.compile("[A-Za-z0-9@._-]{6,20}");
    private final static Pattern Age = Pattern.compile("[0-9]*");
 
    private final static Pattern Chinese = Pattern.compile("^[\u4e00-\u9fa5]*$");
    private final static Pattern English = Pattern.compile("^[A-Za-z]+$");
 
    private final static Pattern LinkMan = Pattern.compile("^([A-Za-z]+)$|^([\u4e00-\u9fa5]{2,4})$");
    private final static Pattern LoanAmt = Pattern.compile("\\d{1,10}");
    private final static Pattern LoanExpiry = Pattern.compile("^\\d{1,2}$");
    private final static Pattern BizLicense = Pattern.compile("(?!^[a-zA-Z]+$)[0-9a-zA-Z]+");
    private final static Pattern LoanMoney = Pattern.compile("^(\\d{1,10})(\\.\\d{1,2})?$");
 
    private final static Pattern HouseSize = Pattern.compile("^(\\d{1,4})(\\.\\d{1,2})?$");
    private final static Pattern ZonuleName = Pattern.compile("[A-Za-z0-9\u4e00-\u9fa5]+");
 
    private final static Pattern Color = Pattern.compile("[A-Za-z\u4e00-\u9fa5]+");
 
    private final static Pattern PaiLiang = Pattern.compile("^(\\d{1,2})(\\.\\d{1,2})?$");
    private final static Pattern CarMileage = Pattern.compile("^(\\d{1,8})(\\.\\d{1,2})?$");
 
    private final static Pattern Money = Pattern.compile("^(\\d{1,8})(\\.\\d{1,2})?$");
 
    /**
     * 判断给定字符串是否空白串。
     * 空白串是指由空格、制表符、回车符、换行符组成的字符串
     * 若输入字符串为null或空字符串,返回true
     *
     * @param input
     * @return boolean
     */
    public static boolean isEmpty(String input) {
 
        if (input == null || "".equals(input)) {
            return true;
        }
 
        for (int i = 0; i < input.length(); i++) {
            char c = input.charAt(i);
            if (c != ' ' && c != '\t' && c != '\r' && c != '\n') {
                return false;
            }
        }
        return true;
 
    }
 
    /**
     * 判断是不是一个合法的身份证号
     *
     * @param card
     * @return
     */
    public static boolean isIdentityCard(String card) {
 
        if (card == null || "".equals(card))
            return false;
        return identityCard.matcher(card).matches();
 
    }
 
    /**
     * 判断是不是一个合法的电子邮件地址
     *
     * @param email
     * @return
     */
    public static boolean isEmail(String email) {
        if (TextUtils.isEmpty(email))
            return false;
        return emailer.matcher(email).matches();
    }
 
    /**
     * 判断是不是一个合法的用户密码
     *
     * @return
     */
    public static boolean isPassword(String password) {
        if (TextUtils.isEmpty(password))
            return false;
        return passer.matcher(password).matches();
    }
 
    /**
     * 判断是不是一个合法的用户名
     *
     * @param custName
     * @return true为合法的用户名
     */
    public static boolean isUser(String custName) {
 
        if (custName == null || "".equals(custName)) {
            return false;
        }
        return user.matcher(custName).matches();
 
    }
 
    public static boolean ispureNumber(String custName) {
        return pureNumber.matcher(custName).matches();
    }
 
    /**
     * 判断是不是一个合法的年龄
     *
     * @param age
     * @return
     */
    public static boolean isAge(String age) {
        return Age.matcher(age).matches();
    }
 
    /**
     * 这个方法只支持最大长度为32的随机字符串,如要支持更大长度的,可以适当修改此方法,如前面补、后面补,或者多个uuid相连接
     * @param length
     * @return
     */
    public static String toFixedLengthStringByUUID(int length) {
 
        //也可以通过UUID来随机生成
        UUID uuid = UUID.randomUUID();
        return uuid.toString().replace("-", "").substring(0, length);
    }
 
    /**
     * 判断是不是一个合法的手机号码
     */
    public static boolean isPhone(String phone) {
        if (TextUtils.isEmpty(phone))
            return false;
        return phoner.matcher(phone).matches();
    }
 
    /**
     * 判断是不是一个合法的银行卡号
     *
     * @return
     */
    public static boolean isBankCard(String cardNum) {
        if (cardNum == null || "".equals(cardNum))
            return false;
        return bankCarder.matcher(cardNum).matches();
    }
 
    /**
     * 数字类型的金额格式化成字符串
     */
    public static String toMoney(Double obj) {
        if (obj == null) {
            return "0.00";
        }
        DecimalFormat df = new DecimalFormat("#,#0.00");
        return df.format(obj);
    }
 
    /**
     * 数字格式化,保留两位小数
     *
     * @param obj
     * @return
     */
    public static String douFormat(double obj) {
        DecimalFormat df = new DecimalFormat("0.00");
        return df.format(obj);
    }
 
    /**
     * 获取输入的字符长度,汉字为两个字符
     *
     * @param value
     * @return
     */
    public static int stringLength(String value) {
        int valueLength = 0;
        String chinese = "[\u4e00-\u9fa5]";
        for (int i = 0; i < value.length(); i++) {
            String temp = value.substring(i, i + 1);
            if (temp.matches(chinese)) {
                valueLength += 2;
            } else {
                valueLength += 1;
            }
        }
        return valueLength;
    }
 
    /**
     * 将字符串转化为double类型
     *
     * @param value
     * @return
     */
    public static double StringToDouble(String value) {
        try {
            if (value == null || "".equals(value))
                return 0.00;
            return Double.parseDouble(value);
        }catch (NumberFormatException e){
            return 0;
        }
 
    }
 
    /**
     * 字符串转整数
     *
     * @param str
     * @return
     */
    public static int StringToInt(String str) {
        try {
            return Integer.parseInt(str);
        } catch (Exception e) {
        }
        return 0;
    }
 
    /**
     * 字符串转long
     *
     * @param str
     * @return
     */
    public static long StringToLong(String str) {
        try {
            return Long.parseLong(str);
        } catch (Exception e) {
        }
        return 0;
    }
 
 
    /**
     * 判断字符串是否是中文字符
     *
     * @return boolean false 字符串含有其他非中文字符  true 字符串都是中文字符串
     */
    public static boolean isChinese(String str) {
 
        if (null == str || "".equals(str.trim())) {
            return false;
        }
 
        for (int i = 0; i < str.length(); i++) {
 
            if (!isChinese(str.charAt(i))) {
                return false;
            }
 
        }
 
        return true;
 
    }
 
    /**
     * 判断字符是否是中文字符
     *
     * @return boolean false  字符不是中文字符  true 字符是中文字符
     */
    public static boolean isChinese(char c) {
 
        int v = (int) c;
        return (v >= 19968 && v < 171941);
 
    }
 
    /**
     * 中文汉字
     *
     * @param str
     * @return
     */
    public static boolean isC(String str) {
 
        if (null == str || "".equals(str.trim())) {
            return false;
        }
        return Chinese.matcher(str).matches();
 
    }
 
    /**
     * 英文
     *
     * @param str
     * @return
     */
    public static boolean isEnglish(String str) {
 
        if (null == str || "".equals(str.trim())) {
            return false;
        }
        return English.matcher(str).matches();
 
    }
 
    /**
     * 金额
     *
     * @param str
     * @return
     */
    public static boolean isMoney(String str) {
 
        if (null == str || "".equals(str.trim())) {
            return false;
        }
        return Money.matcher(str).matches();
 
    }
 
 
    /**
     * 判断名字是否含有英文特殊字符(功能应未完善,应该包含中文特殊字符)
     *
     * @return boolean false 名字不合法,含有英文特殊字符     true 名字合法 ,不含有英文特殊字符
     */
    public static boolean isNameLegal(String str) {
 
        String temp = "@_\\_|_~_`_!_#_$_%_^_&_*_(_)_{_}_[_]_;_:_'_\"_,_<_._>_/_?_-_¥_!_ @_#_¥_%_&_*_(_)_——_+_?_~_·_;_:|、_{_}_【_】_1_2_3_4_5_6_7_8_9_0_“_”_…_、_:_《_》_℃_€_£_=_÷_,_。";
        String[] strs = temp.split("_");
 
        for (int i = 0; i < strs.length; i++) {
 
            if (str.contains(strs[i])) {
                return false;
            }
 
        }
        return true;
 
    }
 
 
    /**
     * 判断是否符合联系人格式
     *
     * @param str
     * @return
     */
    public static boolean isLinkMan(String str) {
 
        if (null == str || "".equals(str.trim())) {
            return false;
        }
        return LinkMan.matcher(str).matches();
 
    }
 
 
    /**
     * 融资金额
     *
     * @param str
     * @return
     */
    public static boolean isLoanAmt(String str) {
 
        if (TextUtils.isEmpty(str)) {
            return false;
        }
        return LoanAmt.matcher(str).matches();
 
    }
 
 
    /**
     * 贷款期限
     *
     * @param str
     * @return
     */
    public static boolean isLoanExpiry(String str) {
 
        if (null == str || "".equals(str.trim())) {
            return false;
        }
        return LoanExpiry.matcher(str).matches();
 
    }
 
 
    /**
     * 营业执照
     *
     * @param str
     * @return
     */
    public static boolean isBizLicense(String str) {
 
        if (null == str || "".equals(str.trim())) {
            return false;
        }
        return BizLicense.matcher(str).matches();
 
    }
 
 
    /**
     * 资金
     *
     * @param str
     * @return
     */
    public static boolean isLoanMoney(String str) {
 
        if (null == str || "".equals(str.trim())) {
            return false;
        }
        return LoanMoney.matcher(str).matches();
 
    }
 
 
    /**
     * 房屋面积
     *
     * @param str
     * @return
     */
    public static boolean isHouseSize(String str) {
 
        if (null == str || "".equals(str.trim())) {
            return false;
        }
        return HouseSize.matcher(str).matches();
    }
 
    public static String cutOffCardNum(String id) {
        if (id.length() == 18) {
            return new StringBuilder().append(id.substring(0, 3)).append("****").append(id.substring(14, 18)).toString();
        } else if (id.length() == 15) {
            return new StringBuilder().append(id.substring(0, 3)).append("****").append(id.substring(11, 15)).toString();
        } else {
            return id;
        }
    }
 
    public static String cutOffPhone(String phone) {
        if (isPhone(phone)) {
            return new StringBuilder().append(phone.substring(0, 3)).append("****").append(phone.substring(8, 11)).toString();
        } else {
            return phone;
        }
    }
 
 
    /**
     * 小区名称
     *
     * @param str
     * @return
     */
    public static boolean isZonuleName(String str) {
 
        if (null == str || "".equals(str.trim())) {
            return false;
        }
        return ZonuleName.matcher(str).matches();
 
    }
 
 
    /**
     * 排量
     *
     * @param str
     * @return
     */
    public static boolean isPaiLiang(String str) {
 
        if (null == str || "".equals(str.trim())) {
            return false;
        }
        return PaiLiang.matcher(str).matches();
 
    }
 
    /**
     * 汽车颜色
     *
     * @param str
     * @return
     */
    public static boolean isColor(String str) {
 
        if (null == str || "".equals(str.trim())) {
            return false;
        }
        return Color.matcher(str).matches();
 
    }
 
 
    /**
     * 汽车里程
     *
     * @param str
     * @return
     */
    public static boolean isCarMileage(String str) {
 
        if (null == str || "".equals(str.trim())) {
            return false;
        }
        return CarMileage.matcher(str).matches();
    }
 
 
    public static String replaceEach(final String text, final String[] searchList, final String[] replacementList) {
        return replaceEach(text, searchList, replacementList, false, 0);
    }
 
    public static String dateFormat(String date){
        if(TextUtils.isEmpty(date)){
            return "";
        }
        return date.substring(0,4)+"-"+date.substring(4,6)+"-"+date.substring(6,8)+" "+date.substring(8,10)+":"+date.substring(10,12)+":"+date.substring(12);
    }
 
    public static String simpleDateFormat(String date){
        if(date==null){
            return "";
        }
        return date.substring(0,4)+"-"+date.substring(4,6)+"-"+date.substring(6,8);
    }
 
    public static String timeToString(long time){
        SimpleDateFormat format =  new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String d = format.format(new Date(time));
        return d;
    }
 
    /**
     * <p>
     * Replaces all occurrences of Strings within another String.
     * </p>
     * <p/>
     * <p>
     * A {@code null} reference passed to this method is a no-op, or if
     * any "search string" or "string to replace" is null, that replace will be
     * ignored.
     * </p>
     * <p/>
     * <pre>
     *  StringUtils.replaceEachRepeatedly(null, *, *) = null
     *  StringUtils.replaceEachRepeatedly("", *, *) = ""
     *  StringUtils.replaceEachRepeatedly("aba", null, null) = "aba"
     *  StringUtils.replaceEachRepeatedly("aba", new String[0], null) = "aba"
     *  StringUtils.replaceEachRepeatedly("aba", null, new String[0]) = "aba"
     *  StringUtils.replaceEachRepeatedly("aba", new String[]{"a"}, null) = "aba"
     *  StringUtils.replaceEachRepeatedly("aba", new String[]{"a"}, new String[]{""}) = "b"
     *  StringUtils.replaceEachRepeatedly("aba", new String[]{null}, new String[]{"a"}) = "aba"
     *  StringUtils.replaceEachRepeatedly("abcde", new String[]{"ab", "d"}, new String[]{"w", "t"}) = "wcte"
     *  (example of how it repeats)
     *  StringUtils.replaceEachRepeatedly("abcde", new String[]{"ab", "d"}, new String[]{"d", "t"}) = "tcte"
     *  StringUtils.replaceEachRepeatedly("abcde", new String[]{"ab", "d"}, new String[]{"d", "ab"}) = IllegalStateException
     * </pre>
     *
     * @param text            text to search and replace in, no-op if null
     * @param searchList      the Strings to search for, no-op if null
     * @param replacementList the Strings to replace them with, no-op if null
     * @return the text with any replacements processed, {@code null} if
     * null String input
     * @throws IllegalStateException    if the search is repeating and there is an endless loop due
     *                                  to outputs of one being inputs to another
     * @throws IllegalArgumentException if the lengths of the arrays are not the same (null is ok,
     *                                  and/or size 0)
     * @since 2.4
     */
    public static String replaceEach(
            final String text, final String[] searchList, final String[] replacementList, final boolean repeat, final int timeToLive) {
 
        // mchyzer Performance note: This creates very few new objects (one major goal)
        // let me know if there are performance requests, we can create a harness to measure
 
        if (text == null || text.isEmpty() || searchList == null ||
                searchList.length == 0 || replacementList == null || replacementList.length == 0) {
            return text;
        }
 
        // if recursing, this shouldn't be less than 0
        if (timeToLive < 0) {
            throw new IllegalStateException("Aborting to protect against StackOverflowError - " +
                    "output of one loop is the input of another");
        }
 
        final int searchLength = searchList.length;
        final int replacementLength = replacementList.length;
 
        // make sure lengths are ok, these need to be equal
        if (searchLength != replacementLength) {
            throw new IllegalArgumentException("Search and Replace array lengths don't match: "
                    + searchLength
                    + " vs "
                    + replacementLength);
        }
 
        // keep track of which still have matches
        final boolean[] noMoreMatchesForReplIndex = new boolean[searchLength];
 
        // index on index that the match was found
        int textIndex = -1;
        int replaceIndex = -1;
        int tempIndex = -1;
 
        // index of replace array that will replace the search string found
        // NOTE: logic duplicated below START
        for (int i = 0; i < searchLength; i++) {
            if (noMoreMatchesForReplIndex[i] || searchList[i] == null ||
                    searchList[i].isEmpty() || replacementList[i] == null) {
                continue;
            }
            tempIndex = text.indexOf(searchList[i]);
 
            // see if we need to keep searching for this
            if (tempIndex == -1) {
                noMoreMatchesForReplIndex[i] = true;
            } else {
                if (textIndex == -1 || tempIndex < textIndex) {
                    textIndex = tempIndex;
                    replaceIndex = i;
                }
            }
        }
        // NOTE: logic mostly below END
 
        // no search strings found, we are done
        if (textIndex == -1) {
            return text;
        }
 
        int start = 0;
 
        // get a good guess on the size of the result buffer so it doesn't have to double if it goes over a bit
        int increase = 0;
 
        // count the replacement text elements that are larger than their corresponding text being replaced
        for (int i = 0; i < searchList.length; i++) {
            if (searchList[i] == null || replacementList[i] == null) {
                continue;
            }
            final int greater = replacementList[i].length() - searchList[i].length();
            if (greater > 0) {
                increase += 3 * greater; // assume 3 matches
            }
        }
        // have upper-bound at 20% increase, then let Java take over
        increase = Math.min(increase, text.length() / 5);
 
        final StringBuilder buf = new StringBuilder(text.length() + increase);
 
        while (textIndex != -1) {
 
            for (int i = start; i < textIndex; i++) {
                buf.append(text.charAt(i));
            }
            buf.append(replacementList[replaceIndex]);
 
            start = textIndex + searchList[replaceIndex].length();
 
            textIndex = -1;
            replaceIndex = -1;
            tempIndex = -1;
            // find the next earliest match
            // NOTE: logic mostly duplicated above START
            for (int i = 0; i < searchLength; i++) {
                if (noMoreMatchesForReplIndex[i] || searchList[i] == null ||
                        searchList[i].isEmpty() || replacementList[i] == null) {
                    continue;
                }
                tempIndex = text.indexOf(searchList[i], start);
 
                // see if we need to keep searching for this
                if (tempIndex == -1) {
                    noMoreMatchesForReplIndex[i] = true;
                } else {
                    if (textIndex == -1 || tempIndex < textIndex) {
                        textIndex = tempIndex;
                        replaceIndex = i;
                    }
                }
            }
            // NOTE: logic duplicated above END
 
        }
        final int textLength = text.length();
        for (int i = start; i < textLength; i++) {
            buf.append(text.charAt(i));
        }
        final String result = buf.toString();
        if (!repeat) {
            return result;
        }
 
        return replaceEach(result, searchList, replacementList, repeat, timeToLive - 1);
    }
 
    /**
     * 半角转全角 * @param
     input String. * @return
     全角字符串. */
    public static String toSBC(String input) {
        char c[] = input.toCharArray();
        for (int i = 0; i < c.length; i++) {
            if(c[i] == ' ') {
                c[i] = '\u3000';
            }else if(c[i] < '\177') {
                c[i] = (char) (c[i] + 65248);
            }
        }
        return new String(c);
    }
 
    public static String hashToStr(HashMap<String, Object> map){
        JSONObject clientKey = new JSONObject();
        //  将map中的key-value关系存入到set集合中,再使用Map.Entry
        Set entrySet = map.entrySet(); // key-value的set集合
        Iterator it2 = entrySet.iterator();
        while(it2.hasNext()){
            Map.Entry me = (Map.Entry) it2.next();
            Object k = me.getKey();
            Object v = me.getValue();
            try {
                clientKey.put((String)k,v);
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
        return String.valueOf(clientKey);
    }
 
    /**
     * 秒转为分钟小时
     * @return
     *//*
    public static String sToMH(int s) {
        int h=0;
        int m=0;
        StringBuilder builder=new StringBuilder();
        if(s/3600>0){
            h=s/3600;
            builder.append(h+MApplication.mContext.getResources().getString(R.string.time_h));
            s-=3600*h;
        }
        if(s/60>0){
            m=s/60;
            builder.append(m+MApplication.mContext.getResources().getString(R.string.time_m));
            s-=m*60;
        }else {
            if(h!=0){
                builder.append(0+MApplication.mContext.getResources().getString(R.string.time_m));
            }
        }
        builder.append(s+ MApplication.mContext.getResources().getString(R.string.time_s));
        return builder.toString();
    }*/
 
    /**
     * 影藏手机中间4位
     * @return
     */
    public static String phoneToHind(String s) {
        if(s==null){
            return "";
        }
        if(s.length()==11){
            s=s.substring(0,3)+"****"+s.substring(7);
        }
        return s;
    }
 
    /**
     * 影藏银行卡号码,保留后4位
     * @return
     */
    public static String bankNumToHind(String s) {
        if(s==null){
            return "";
        }
        if(s.length()>4){
            int size=s.length();
            String s1=s.substring(size-4);
            StringBuffer buffer=new StringBuffer();
            for(int i=0;i<size-4;i++){
                buffer.append("*");
            }
            s=buffer.toString()+s1;
        }
        return s;
    }
 
    /**
     * SHA加密
     * @return 加密之后的密文
     */
    public static String shaEncrypt(String data) {
        byte[] datas=data.getBytes();
        String result = null;
        try {
            //根据给定的字节数组构造一个密钥,第二参数指定一个密钥算法的名称
            SecretKeySpec signinKey = new SecretKeySpec("Gu5t9xGARNpq86cd98joQYCN3Cozk1qA".getBytes(), "HmacSHA1");
            //生成一个指定 Mac 算法 的 Mac 对象
            Mac mac = Mac.getInstance("HmacSHA1");
            //用给定密钥初始化 Mac 对象
            mac.init(signinKey);
            //完成 Mac 操作
            byte[] rawHmac = mac.doFinal(datas);
            result = new String();
        } catch (NoSuchAlgorithmException e) {
            System.err.println(e.getMessage());
        } catch (Exception e) {
            System.err.println(e.getMessage());
        }
        if (null != result) {
            return result;
        } else {
            return null;
        }
    }
 
    /**
     * byte数组转换为16进制字符串
     *
     * @param bts
     *            数据源
     * @return 16进制字符串
     */
    public static String bytes2Hex(byte[] bts) {
        String des = "";
        String tmp = null;
        for (int i = 0; i < bts.length; i++) {
            tmp = (Integer.toHexString(bts[i] & 0xFF));
            if (tmp.length() == 1) {
                des += "0";
            }
            des += tmp;
        }
        return des;
    }
 
    // 加密
    public static String encrypt(String sSrc){
        String sKey = "abcdef0123456789";
        String ivParameter = "0123456789abcdef";
        String result = "";
        try {
            Cipher cipher;
            cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
            byte[] raw = sKey.getBytes();
            SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
            IvParameterSpec iv = new IvParameterSpec(ivParameter.getBytes());// 使用CBC模式,需要一个向量iv,可增加加密算法的强度
            cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv);
            byte[] encrypted = cipher.doFinal(sSrc.getBytes("utf-8"));
            result = new String();
        } catch (Exception e) {
            e.printStackTrace();
        }
        // 此处使用BASE64做转码。
        return result;
 
    }
 
    // 解密
    public static String decrypt(String sSrc){
        if(sSrc==null){
            return "";
        }
        String sKey = "abcdef0123456789";
        String ivParameter = "0123456789abcdef";
        try {
            byte[] raw = sKey.getBytes("ASCII");
            SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
            IvParameterSpec iv = new IvParameterSpec(ivParameter.getBytes());
            cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv);
            byte[] encrypted1 = "".getBytes();// 先用base64解密
            byte[] original = cipher.doFinal(encrypted1);
            String originalString = new String(original, "utf-8");
            raw=null;
            encrypted1=null;
            original=null;
            return "{\"data\":"+originalString+"}";
        } catch (Exception ex) {
            ex.printStackTrace();
            return null;
        }
    }
 
    // 解密
    public static String decryptNew(String sSrc){
        if(sSrc==null){
            return "";
        }
        String sKey = "abcdef0123456789";
        String ivParameter = "0123456789abcdef";
        try {
            byte[] raw = sKey.getBytes("ASCII");
            SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
            IvParameterSpec iv = new IvParameterSpec(ivParameter.getBytes());
            cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv);
            byte[] encrypted1 = "".getBytes();// 先用base64解密
            byte[] original = cipher.doFinal(encrypted1);
            String originalString = new String(original, "utf-8");
            raw=null;
            encrypted1=null;
            original=null;
            return originalString;
        } catch (Exception ex) {
            ex.printStackTrace();
            return null;
        }
    }
 
    // 解密
    public static String decryptNoData(String sSrc){
        if(sSrc==null){
            return "";
        }
        String sKey = "abcdef0123456789";
        String ivParameter = "0123456789abcdef";
        try {
            byte[] raw = sKey.getBytes("ASCII");
            SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
            IvParameterSpec iv = new IvParameterSpec(ivParameter.getBytes());
            cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv);
            byte[] encrypted1 = "".getBytes();// 先用base64解密
            byte[] original = cipher.doFinal(encrypted1);
            String originalString = new String(original, "utf-8");
            raw=null;
            encrypted1=null;
            original=null;
            return originalString+"}";
        } catch (Exception ex) {
            ex.printStackTrace();
            return null;
        }
    }
 
    /**
     *
     * @param str
     * @return
     * @throws NumberFormatException
     */
    public static String inputPriceFormatter(String str)throws NumberFormatException {
        String handledStr = null;
        //小数形式的输入
        if(isValid(str).contains(".")){
            float f_price = Float.valueOf(isValid(str));
            handledStr = Float.toString(f_price);
            //整数形式的输入
        }else{
            int i_price = Integer.valueOf(isValid(str));
            handledStr = Integer.toString(i_price);
        }
        return isValid(handledStr);
    }
    /**
     * 判断是否为空和空串,以NumberFormatException的方式集中处理
     * @param str
     * @return
     * @throws NumberFormatException
     */
    public static String isValid(String str) throws NumberFormatException {
        if(str == null || "".equals(str))
            throw new NumberFormatException();
        return str;
    }
 
    // get rand color
    public static String getRandColorCode() {
        String r,g,b;
        Random random = new Random();
        r = Integer.toHexString(random.nextInt(256)).toUpperCase();
        g = Integer.toHexString(random.nextInt(256)).toUpperCase();
        b = Integer.toHexString(random.nextInt(256)).toUpperCase();
 
        r = r.length() == 1 ? "0" + r : r ;
        g = g.length() == 1 ? "0" + g : g ;
        b = b.length() == 1 ? "0" + b : b ;
 
        return "0xFF"+r + g + b;
    }
 
    public static boolean isCEN(String txt) {
 
        Pattern p = Pattern.compile("[0-9]*");
        Matcher m = p.matcher(txt);
        if (m.matches()) {
            return true;
        }
        p = Pattern.compile("[a-zA-Z]");
        m = p.matcher(txt);
        if (m.matches()) {
            return true;
        }
        p = Pattern.compile("[\u4e00-\u9fa5]");
        m = p.matcher(txt);
        if (m.matches()) {
            return true;
        }
        return false;
    }
 
    public static boolean isLetterOrNum(String txt) {
 
        Pattern p = Pattern.compile("[0-9]*");
        Matcher m = p.matcher(txt);
        if (m.matches()) {
            return true;
        }
        p = Pattern.compile("[a-zA-Z]");
        m = p.matcher(txt);
        if (m.matches()) {
            return true;
        }
        m = p.matcher(txt);
        if (m.matches()) {
            return true;
        }
        return false;
    }
 
    public static boolean isNumber(String txt) {
        Pattern p = Pattern.compile("[0-9]*");
        Matcher m = p.matcher(txt);
        if (m.matches()) {
            return true;
        }
        return false;
    }
 
    /**
     * 日期转换成Java字符串
     * @param date
     * @return str
     */
    public static String DateToStr(Date date) {
 
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String str = format.format(date);
        return str;
    }
 
    public static String getHM() {
 
        SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss");
        String str = format.format(new Date());
        return str;
    }
 
    public static String DateToStrYMD(Date date) {
 
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String str = format.format(date);
        return str;
    }
 
    /**
     * 字符串转换成日期
     * @param str
     * @return date
     */
    public static Date StrToDate(String str) {
 
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = null;
        try {
            date = format.parse(str);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }
 
    public static String removeWhiteSpace(String inputStr) {
        String outputStr = "";
        if (inputStr != null && !inputStr.isEmpty()) {
            //移除所有换行和空格
            outputStr = inputStr.replaceAll("\\s+", "");
        }
        return outputStr;
    }
}