Helius
2021-06-23 0e7f27e131c3c0862d35111f8221a096f4641465
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
package com.xzx.gc.common.utils;
 
import cn.hutool.core.codec.Base64;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.text.StrBuilder;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.StrUtil;
 
import java.math.BigDecimal;
import java.util.regex.Pattern;
 
/**
 * @Description:
 * @Date: 2018/7/17
 * @Author: wcf
 */
public class StringUtils extends org.apache.commons.lang3.StringUtils{
 
    /**
     * 是否是base64编码
     * @param str
     * @return
     */
    public static boolean isBase64(String str) {
        if(StrUtil.isBlank(str))return false;
        if(str.length()==4&&NumberUtil.isNumber(str))return false;
        String base64Pattern = "^([A-Za-z0-9+/]{4})*([A-Za-z0-9+/]{4}|[A-Za-z0-9+/]{3}=|[A-Za-z0-9+/]{2}==)$";
        return Pattern.matches(base64Pattern, str);
    }
 
 
    /**
     * 是否包含中文
     * @param strName
     * @param firstLetter  是否只查询第一个字符
     * @return
     */
    public static boolean isContainChinese(String strName,boolean firstLetter) {
        char[] ch = strName.toCharArray();
        for (int i = 0; i < ch.length; i++) {
            char c = ch[i];
            if(firstLetter){
                if (isChinese(c)) {
                    return true;
                }else {
                    return false;
                }
            }else {
                if (isChinese(c)) {
                    return true;
                }
            }
 
        }
        return false;
    }
 
    // 根据Unicode编码完美的判断中文汉字和符号
    private 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.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_B
                || ub == Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION
                || ub == Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS
                || ub == Character.UnicodeBlock.GENERAL_PUNCTUATION) {
            return true;
        }
        return false;
    }
 
    /**
     * 将数字转换为万的单位
     * @param number
     * @return
     */
    public static String intChange2Str(int number) {
        String str = "";
        if (number <= 0) {
            str = "0";
        } else if (number < 10000) {
            str = number+"";
        } else {
            double d = (double) number;
            double num = d / 10000;//1.将数字转换成以万为单位的数字
 
            BigDecimal b = new BigDecimal(num);
            double f1 = b.setScale(1,BigDecimal.ROUND_HALF_UP).doubleValue();//2.转换后的数字四舍五入保留小数点后一位;
            str = f1 + "万";
        }
        return str;
    }
 
    /**
     * 截取掉最后一个字符串
     * @param strBuilder
     * @param charcode
     * @return
     */
    public static String removeLast(StrBuilder strBuilder, String charcode){
        if(strBuilder.length()>0){
            String s = strBuilder.toString();
            String s1 = StrUtil.removeSuffix(s, charcode);
            return s1;
        }
        return strBuilder.toString();
    }
 
    /**
     * 净化字符
     * @param str
     * @return
     */
    public static String cleanStr(String str){
        if(StrUtil.isNotBlank(str)){
            str=str .replaceAll("\\s*", "");
            str=str.replaceAll("/n","");
        }
        return str;
    }
 
    /**
     * 解码字符串
     * @param str
     * @return
     */
    public static  String decode(String str){
        if(StrUtil.isNotBlank(str)&&isBase64(str)){
            return Base64.decodeStr(str);
        }
        return str;
    }
 
    public static  String encode(String str){
        if(StrUtil.isNotBlank(str)){
            return Base64.encode(str);
        }
        return str;
    }
 
    public static String addZero(int num){
        if(num>0&&num<10&&!Convert.toStr(num).startsWith("0")){
            return "0"+num;
        }
        return num+"";
    }
}