admin
2018-12-19 b91b38942de4a865f105c4ca1e7d5ffce1b60b66
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
package org.yeshi.utils;
 
import java.io.UnsupportedEncodingException;
import java.lang.reflect.Method;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
 
import org.apache.commons.codec.binary.Base64;
 
import sun.misc.BASE64Encoder;
 
public class StringUtil {
 
    // 判断是否为手机号码
    public static boolean isMobile(String mobile) {
 
        String regex = "^((13[0-9])|(17[0-9])|(15[^4,\\D])|(18[0-9]))\\d{8}$";
        Pattern p = Pattern.compile(regex);
        Matcher m = p.matcher(mobile);
 
        if (mobile == null || mobile.equals("") || mobile.length() != 11) {
 
            return false;
 
        } else {
            return m.find();
        }
    }
 
    /**
     * 获取数字和字母的随机码
     * 
     * @param count
     * @return
     */
    public static String getRandomCode(int count) {
        String sts = "0123456789abcdefghijklmnopqrsduvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
        String code = "";
        for (int i = 0; i < count; i++) {
            int p = (int) (Math.random() * 62);
            code += sts.charAt(p);
        }
        return code;
    }
 
    /**
     * 获取6位数字验证码
     * 
     * @return
     */
    public static String getVerifyCode() {
        String sts = "0123456789";
        String code = "";
        for (int i = 0; i < 6; i++) {
            int p = (int) (Math.random() * 10);
            code += sts.charAt(p);
        }
        return code;
    }
 
    /**
     * 判断字符串是否为空
     * 
     * @param text
     * @return
     */
    public static boolean isNullOrEmpty(String text) {
        if (text == null || text.length() == 0 || text.equalsIgnoreCase("null")) {
            return true;
        }
        return false;
    }
 
    /**
     * 32为MD5小写加密
     * 
     * @param st
     * @return
     */
    public static String Md5(String st) {
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            try {
                md.update(st.getBytes("UTF-8"));
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
            byte b[] = md.digest();
 
            int i;
 
            StringBuffer buf = new StringBuffer("");
            for (int offset = 0; offset < b.length; offset++) {
                i = b[offset];
                if (i < 0)
                    i += 256;
                if (i < 16)
                    buf.append("0");
                buf.append(Integer.toHexString(i));
            }
            return buf.toString();
            // LogUtil.i("result: " + buf.toString());// 32λ�ļ���
            // LogUtil.i("result: " + buf.toString().substring(8,
            // 24));// 16λ�ļ���
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return null;
    }
 
    /**
     * 将null转为空字符串
     * 
     * @param st
     * @return
     */
    public static String getString(String st) {
        return isNullOrEmpty(st) ? "" : st;
    }
 
    /**
     * 将整数转为字符串
     * 
     * @param i
     * @return
     */
    public static String getString(int i) {
        return getString(i + "");
    }
 
    /**
     * 将长整形转为字符串
     * 
     * @param i
     * @return
     */
    public static String getString(long i) {
        return getString(i + "");
    }
 
    /**
     * 获取电话号码的隐藏格式
     * 
     * @param mobile
     * @return
     */
    public static String getSubMobile(String mobile) {
        if (!StringUtil.isNullOrEmpty(mobile) && mobile.length() > 10) {
            return mobile.subSequence(0, 3).toString() + "****" + mobile.subSequence(7, mobile.length()).toString();
        } else
            return mobile;
    }
 
    /**
     * BASE64加密
     * 
     * @param b
     * @return
     */
    @SuppressWarnings("restriction")
    public static String getBase64FromByte(byte[] b) {
 
        String s = null;
        if (b != null) {
            s = new BASE64Encoder().encode(b);
        }
        return s;
    }
 
    /**
     * 提取字符串中的所有数字
     * 
     * @param st
     * @return
     */
    public static String getNumberFromString(String st) {
        String number = "";
        Pattern p = Pattern.compile("[0-9\\.]+");
        Matcher m = p.matcher(st);
 
        while (m.find()) {
            number += (m.group() + ",");
        }
        return number;
    }
 
    /**
     * 将字符串转为UTF-8编码
     * 
     * @param resource
     * @param chactor
     * @return
     */
    public static String getUTF8String(String resource, String chactor) {
        if (!isNullOrEmpty(resource)) {
            try {
                return new String(resource.getBytes(chactor), "UTF-8");
            } catch (UnsupportedEncodingException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        return "";
    }
 
    /**
     * Base64加密
     * 
     * @param str
     * @return
     * @throws Exception
     */
    @SuppressWarnings({ "unchecked", "rawtypes" })
    public static String getBase64String(String str) throws Exception {
        byte[] input = str.getBytes("UTF-8");
        Class clazz = Class.forName("com.sun.org.apache.xerces.internal.impl.dv.util.Base64");
        Method mainMethod = clazz.getMethod("encode", byte[].class);
        mainMethod.setAccessible(true);
        Object retObj = mainMethod.invoke(null, new Object[] { input });
        return (String) retObj;
    }
 
    /**
     * BASE64解密
     * 
     * @param s
     * @return
     */
    public static String getFromBase64(String s) {
        byte[] b = null;
        String result = null;
        if (s != null) {
            final Base64 base64 = new Base64();
            try {
                b = base64.decode(s.getBytes("UTF-8"));
                result = new String(b, "utf-8");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return result;
    }
 
    /**
     * SHA1加密
     * 
     * @param decript
     * @return
     */
    public static String SHA1(String decript) {
        try {
            MessageDigest digest = java.security.MessageDigest.getInstance("SHA-1");
            digest.update(decript.getBytes());
            byte messageDigest[] = digest.digest();
            // Create Hex String
            StringBuffer hexString = new StringBuffer();
            // 字节数组转换�?十六进制 �?
            for (int i = 0; i < messageDigest.length; i++) {
                String shaHex = Integer.toHexString(messageDigest[i] & 0xFF);
                if (shaHex.length() < 2) {
                    hexString.append(0);
                }
                hexString.append(shaHex);
            }
            return hexString.toString();
 
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return "";
    }
 
    public static long[] parseLong(String[] arr) {
        long[] lArr = new long[arr.length];
        int ii = 0;
        for (String str : arr) {
            lArr[ii] = Long.parseLong(str);
            ii++;
        }
        return lArr;
    }
 
}