Helius
2020-12-09 660fba5b40303dd661afcc2e2bd54a18d4f53c5c
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
package com.matrix.core.tools;
 
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Random;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
 
import com.matrix.core.exception.GlobleException;
import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.HanyuPinyinVCharType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;
import org.apache.commons.collections.CollectionUtils;
 
/**
 * 字符串操作类,转换数据类型,切割字符串,对象比较等操作
 *
 * @author JIANGYOUYAO
 * @email 935090232@qq.com
 * @date 2017年11月29日
 */
public class StringUtils {
 
    private static final String REPLACE_CHAR = "*";
 
    private static final int SAVA_CHAR_LENGTH = 4;
 
    private static final String EMPTY = "";
 
    /**
     * 将字符串中的某些值用指定字符代替
     *
     * @param str         原始字符串
     * @param saveLength  保留位数
     * @param replaceChar 替换成的字符
     * @return
     */
    public static String replaceStr(String str, int saveLength, String replaceChar) {
 
        if (saveLength <= 0) {
            saveLength = SAVA_CHAR_LENGTH;
        }
 
        if (replaceChar == null) {
            replaceChar = REPLACE_CHAR;
        }
 
        if (isBlank(str) || str.length() < saveLength) {
            return "";
        }
 
        char[] chars = str.toCharArray();
        StringBuffer stb = new StringBuffer();
        for (int i = 0; i < chars.length; i++) {
            if (i < (saveLength - 1) / 2 || (chars.length - i - 1) < (saveLength + 2) / 2) {
                stb.append(chars[i]);
            } else {
                stb.append(replaceChar);
            }
        }
 
        return stb.toString();
    }
 
    /**
     * 是否为空,去除首尾空格后比较
     *
     * @param str
     * @return
     */
    public static boolean isBlank(String str) {
 
        if (str == null) {
            return true;
        }
 
        if (EMPTY.equals(str.trim())) {
            return true;
        }
 
        return false;
    }
 
    /**
     * 不为空,去除首尾空格
     *
     * @param str
     * @return
     */
    public static boolean isNotBlank(String str) {
 
        if (str == null) {
            return false;
        }
 
        if (EMPTY.equals(str.trim())) {
            return false;
        }
 
        return true;
    }
 
    /**
     * 去除字符串首尾空格
     *
     * @param str
     * @return
     */
    public static String trim(String str) {
 
        if (isNotBlank(str)) {
            return str.trim();
        }
 
        return str;
    }
 
    /**
     * 两个字符串是否相等
     *
     * @param a
     * @param b
     * @return
     */
    public static boolean equals(String a, String b) {
 
        if (a == null && b == null) {
            return true;
        } else if (a != null && b != null) {
            if (a.trim().equals(b.trim())) {
                return true;
            }
        }
 
        return false;
    }
 
    /**
     * 两个字符串是否相等
     *
     * @param a
     * @param b
     * @return
     */
    public static boolean notEquals(String a, String b) {
 
        return !equals(a, b);
    }
 
    /**
     * 忽略大小写后两个字符串是否相等
     *
     * @param a
     * @param b
     * @return
     */
    public static boolean equalsIgnoreCase(String a, String b) {
        if (a == null && b == null) {
            return true;
        } else if (a != null && b != null) {
            return a.toUpperCase().equals(b.toUpperCase());
        } else {
            return false;
        }
    }
 
    /**
     * 忽略大小写后两个字符串是否相等
     *
     * @param a
     * @param b
     * @return
     */
    public static boolean notEqualsIgnoreCase(String a, String b) {
 
        return !equalsIgnoreCase(a, b);
    }
 
    /**
     * 用字符串split风格字符串str成数组
     *
     * @param str
     * @param split
     * @return
     */
    public static String[] stringToArray(String str, String split) {
 
        if (isBlank(str)) {
            return null;
        } else {
            return str.split(split);
        }
    }
 
    /**
     * 如果值为null或者是空格则显示空
     *
     * @param str
     * @return
     */
    public static String getString(String str) {
 
        if (isBlank(str)) {
            return EMPTY;
        }
 
        return str.trim();
    }
 
    /**
     * 字符串如果为空或空格则返回null
     *
     * @param str
     * @return
     */
    public static String getNull(String str) {
 
        if (isNotBlank(str)) {
            return str;
        }
 
        return null;
    }
 
    /**
     * 填充字符串到指定长度
     *
     * @param source   原始字符串
     * @param length   填充到长度
     * @param chars    填充字符
     * @param fillLeft true-左填充,false-右填充
     * @return
     */
    public static String fillStringWithChar(String source, int length, char chars, boolean fillLeft) {
 
        if (StringUtils.isNotBlank(source) && length > 0) {
            int realLength = length - source.length();
 
            StringBuffer stb = new StringBuffer();
            for (; realLength > 0; realLength--) {
                stb.append(chars);
            }
 
            if (fillLeft) {
                return stb.toString() + source;
            } else {
                return source + stb.toString();
            }
        }
        return source;
    }
 
    /**
     * 去掉json字符串两头的中括号
     *
     * @param jsonArrStr
     * @return
     */
    public static String getJsonObjStr(String jsonArrStr) {
        String tempString = jsonArrStr.trim();
        if (tempString.startsWith("[") && tempString.endsWith("]")) {
            return tempString.substring(1, tempString.length() - 1);
        } else {
            return tempString;
        }
    }
 
    private static final int MAX_GENERATE_COUNT = 99999;
 
    private static int generateCount = 0;
 
    public static synchronized String getUniqueString() {
 
        if (generateCount > MAX_GENERATE_COUNT) {
            generateCount = 0;
        }
        String uniqueNumber = Long.toString(System.currentTimeMillis()) + Integer.toString(generateCount);
 
        generateCount++;
 
        return uniqueNumber;
    }
 
    /***
     * 针对javascript encodeURIComponent编码后进行解码
     *
     * @param str
     * @return
     * @throws UnsupportedEncodingException
     */
    public static String decode(String str) throws UnsupportedEncodingException {
        return java.net.URLDecoder.decode(str, "UTF-8");
    }
 
    public static String encodingUTF(String str) throws UnsupportedEncodingException {
        if (str == null) {
            return null;
        }
        return new String(str.getBytes("ISO8859-1"), "UTF-8");
    }
 
    /**
     * 获得不重复的32位的随机的大写字符串
     *
     * @Return:String
     * @Author: JiangYouYao
     * @Version: V1.00 (版本号1.0)
     * @Create Date: 2014-8-30(创建日期)
     */
    public static String uuid() {
        return UUID.randomUUID().toString().replace("-", "").toUpperCase();
    }
 
    /**
     * 把一个集合转化成一个字符串,null或size为0时转化为空串
     */
    public static String collToStr(Collection<?> collection, String splitPatten) {
        if (CollectionUtils.isEmpty(collection)) {
            return "";
        }
        Iterator<?> it = collection.iterator();
        StringBuilder sb = new StringBuilder();
        while (it.hasNext()) {
            sb.append(it.next().toString() + splitPatten);
        }
        int lastIndex = sb.lastIndexOf(splitPatten);
        return sb.substring(0, lastIndex);
    }
 
    /**
     * 字符串转集合
     *
     * @param sour        原字符串
     * @param splitPatten 截取规则
     * @return List<String>
     */
    public static List<String> strToColl(String sour, String splitPatten) {
        List<String> list = new ArrayList<>();
        if (sour != null) {
            String[] temps = sour.split(splitPatten);
            for (String string : temps) {
                list.add(string);
            }
        }
        return list;
    }
 
    /**
     * 字符串转集合
     *
     * @param sour        原字符串
     * @param splitPatten 截取规则
     * @return List<String>
     */
    public static List<String> strToCollToString(String sour, String splitPatten) {
        List<String> list = new ArrayList<>();
        if (sour != null) {
            String[] temps = sour.split(splitPatten);
            for (String string : temps) {
                list.add(string);
            }
        }
        return list;
    }
 
    /**
     * 字符串转集合
     *
     * @param sour        原字符串
     * @param splitPatten 截取规则
     * @return List<String>
     */
    public static List<Integer> strToCollToInteger(String sour, String splitPatten) {
        List<Integer> list = new ArrayList<>();
        if (sour != null) {
            String[] temps = sour.split(splitPatten);
            for (String string : temps) {
                list.add(Integer.valueOf(string));
            }
        }
        return list;
    }
 
    /**
     * @Title: getRandomString
     * 获取一个获得一定长度的随机数,包含大写字母及数字 @author:jyy @param length @return @return
     * String 返回类型 @date 2016年7月19日 下午3:01:20 @throws
     */
    public static String getRandomString(int length) {
        String str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        Random random = new Random();
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < length; i++) {
            int number = random.nextInt(str.length());
            sb.append(str.charAt(number));
        }
        return sb.toString();
    }
 
    /**
     * 用逗号分开的存的id是否包含对应的id 如比较 123,123,124 中是否包含12这个主键 比对的方法是 字符串中是否包含"^12$"
     * "^12,.*" 或者 ".+,12$" 或者 ".+,12,$" 或者 ".+,12,.+" 字符串
     */
    public static boolean isContentSet(String id, String setStr) {
        id = id.trim();
        setStr = setStr.trim();
        String reg = "^" + id + "$|^" + id + ",.*|.+," + id + "$|.+," + id + ",$|.+," + id + ",.+";
 
        Pattern pattern = Pattern.compile(reg);
        Matcher matcher = pattern.matcher(setStr);
        boolean result = matcher.matches();
        LogUtil.info("id=[" + id + "] :  setStr =[" + setStr + "] :result=" + result);
        return result;
    }
 
    public static List<Long> strToCollToLong(String sour, String splitPatten) {
        List<Long> list = new ArrayList<>();
        if (isNotBlank(sour)) {
            String[] temps = sour.split(splitPatten);
            for (String string : temps) {
                list.add(Long.parseLong(string));
            }
        }
        return list;
    }
 
 
    /**
     * 检测temp包含多个参数
     *
     * @param temp
     * @param cons
     * @return
     */
    public static boolean checkContainMore(String temp, String... cons) {
        if (temp == null) {
            return false;
        }
        for (String con : cons) {
            if (temp.contains(con)) {
                return true;
            }
        }
        return false;
    }
 
    /**
     * 将字符串转换为整形
     *
     * @param str
     * @return
     */
    public static int stringToInt(String str) {
 
        if (StringUtils.isBlank(str)) {
            return 0;
        }
        try {
            return Integer.valueOf(str).intValue();
        } catch (Exception e) {
            return 0;
        }
    }
 
 
    /**
     * 将文字转为汉语拼音
     *
     * @param chineselanguage 要转成拼音的中文
     */
    public static String toHanyuPinyin(String chineseStr) {
 
        char[] cl_chars = chineseStr.trim().toCharArray();
        String hanyupinyin = "";
        HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
        defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);// 输出拼音全部小写
        defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);// 不带声调
        defaultFormat.setVCharType(HanyuPinyinVCharType.WITH_V);
        try {
            for (int i = 0; i < cl_chars.length; i++) {
                if (String.valueOf(cl_chars[i]).matches("[\u4e00-\u9fa5]+")) {// 如果字符是中文,则将中文转为汉语拼音
                    String py = PinyinHelper.toHanyuPinyinStringArray(cl_chars[i], defaultFormat)[0];
                    hanyupinyin += py;
                } else {// 如果字符不是中文,则不转换
                    if (StringUtils.isNotBlank(cl_chars[i] + "") && !isSpecialChar(cl_chars[i] + "")) {
                        hanyupinyin += cl_chars[i];
                    }
 
                }
            }
        } catch (BadHanyuPinyinOutputFormatCombination e) {
            throw new GlobleException("助记码生成错误" + e.getMessage());
        }
        return hanyupinyin;
    }
 
 
    /**
     * 将首字母转汉语拼音
     *
     * @param chineselanguage 要转成拼音的中文
     */
    public static String toHeadWordHanyuPinyin(String chineseStr) {
 
        char[] cl_chars = chineseStr.trim().toCharArray();
        String hanyupinyin = "";
        HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
        defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);// 输出拼音全部小写
        defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);// 不带声调
        defaultFormat.setVCharType(HanyuPinyinVCharType.WITH_V);
        try {
            for (int i = 0; i < cl_chars.length; i++) {
                if (String.valueOf(cl_chars[i]).matches("[\u4e00-\u9fa5]+")) {// 如果字符是中文,则将中文转为汉语拼音
                    String py = PinyinHelper.toHanyuPinyinStringArray(cl_chars[i], defaultFormat)[0];
                    hanyupinyin += py.substring(0,1);
                } else {// 如果字符不是中文,则不转换
                    if (StringUtils.isNotBlank(cl_chars[i] + "") && !isSpecialChar(cl_chars[i] + "")) {
                        hanyupinyin += cl_chars[i];
                    }
 
                }
            }
        } catch (BadHanyuPinyinOutputFormatCombination e) {
            throw new GlobleException("助记码生成错误" + e.getMessage());
        }
        return hanyupinyin;
    }
 
 
 
 
    /**
     * 判断是否含有特殊字符
     *
     * @param str
     * @return true为包含,false为不包含
     */
    public static boolean isSpecialChar(String str) {
 
        String regEx = "[ -_`~!@#$%^&*()+=|{}':;',\\[\\].<>/?~!@#¥%……&*()——+|{}【】‘;:”“’。,、?]|\n|\r|\t";
        Pattern p = Pattern.compile(regEx);
        Matcher m = p.matcher(str);
        return m.find();
    }
 
    public static boolean isStartWithNumber(String str) {
        Pattern pattern = Pattern.compile("[0-9]*");
        Matcher isNum = pattern.matcher(str.charAt(0) + "");
        if (!isNum.matches()) {
            return false;
        }
        return true;
    }
 
 
 
}