admin
2023-10-08 2c5b6e472c368e80d85b8ea2b461c9ea62981d9b
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
package com.yeshi.appupdate.util;
 
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.StringTokenizer;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
 
import android.content.Context;
import android.content.pm.PackageManager;
import android.os.Build;
import androidx.core.content.ContextCompat;
import android.telephony.TelephonyManager;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.style.ForegroundColorSpan;
import android.text.style.RelativeSizeSpan;
import android.widget.TextView;
 
/**
 * 瀛楃蹇嵎鏂瑰紡锛氬垽鏂瓧绗︿覆<涓枃瀛楃銆侀偖绠便?鎵嬫満鍙枫?绌哄瓧绗︺?鏁存暟銆佹诞鐐规暟>
 */
public class StringUtils {
    public static final String EMPTY_STRING = ""; // 绌哄瓧绗︿覆
 
    public static String join(String[] strs) {
        StringBuilder result = new StringBuilder();
        if (strs != null) {
            for (String str : strs) {
                result.append(str).append(",");
            }
        }
        if (result.length() > 0) {
            return result.substring(0, result.length() - 1);
        }
        return "";
    }
 
    // 瑙f瀽鐭俊鎺ㄩ?鍐呭
    public static Map<String, Object> getParameterMap(String data) {
        Map<String, Object> map = null;
        if (data != null) {
            map = new HashMap<String, Object>();
            String[] params = data.split("&");
            for (int i = 0; i < params.length; i++) {
                int idx = params[i].indexOf("=");
                if (idx >= 0) {
                    map.put(params[i].substring(0, idx),
                            params[i].substring(idx + 1));
                }
            }
        }
        return map;
    }
 
    // 妫?祴瀛楃涓叉槸鍚︾鍚堢敤鎴峰悕
    public static boolean checkingMsg(int len) {
        boolean isValid = true;
        if (5 < len && len < 21) {
            isValid = false;
        } else {
            isValid = true;
        }
        return isValid;
    }
 
    // 妫?祴
    public static boolean isVaild(int len) {
        boolean isValid = true;
        if (1 < len && len < 17) {
            isValid = false;
        }
        return isValid;
    }
 
    // 鍒ゆ柇瀛楃涓叉槸鍚︽槸鏁存暟
    public static boolean isInteger(String aString) {
        try {
            Integer.parseInt(aString);
            return true;
        } catch (NumberFormatException e) {
            return false;
        }
    }
 
    // 鍒ゆ柇瀛楃涓叉槸鍚︽槸娴偣鏁?
    public static boolean isDouble(String value) {
        try {
            Double.parseDouble(value);
            if (value.contains("."))
                return true;
            return false;
        } catch (NumberFormatException e) {
            return false;
        }
    }
 
    // 妫?祴瀛楃涓叉槸鍚︿负涓枃瀛楃
    public static boolean isChinesrChar(String str) {
        if (str.length() < str.getBytes().length) {
            return true;
        } else {
            return false;
        }
    }
 
    // 鍒ゆ柇瀛楃涓叉槸鍚︿负閭
    public static boolean isEmailVaild(String aEmail) {
        boolean isValid = true;
        Pattern pattern = Pattern
                .compile(
                        "^([a-zA-Z0-9]+[_|-|.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|-|.]?)*[a-zA-Z0-9]+\\.[a-zA-Z]{2,3}$",
                        Pattern.CASE_INSENSITIVE);
        Matcher matcher = pattern.matcher(aEmail);
        if (matcher.matches()) {
            isValid = false;
        }
        return isValid;
    }
 
    // 鍒ゆ柇瀛楃涓叉槸鍚︿负鎵嬫満鍙风爜
    public static boolean isMobileNumber(String aTelNumber) {
        Pattern p = Pattern
                .compile("(^1((((3[5-9])|(47)|(5[0-2])|(5[7-9])|(82)|(8[7-8]))\\d{8})|((34[0-8])\\d{7}))$)|(^1((3[0-2])|(5[5-6])|(8[0-6]))\\d{8}$)|(^1((33[0-9])|(349)|(53[0-9])|(80[0-9])|(89[0-9]))\\d{7}$)");
        Matcher m = p.matcher(aTelNumber);
        return m.matches();
    }
 
    // 鏍煎紡鍖栨墜鏈哄彿鐮?
    public static String formatPhoneNum(String aPhoneNum) {
        String first = aPhoneNum.substring(0, 3);
        String end = aPhoneNum.substring(7, 11);
        String phoneNumber = first + "****" + end;
        return phoneNumber;
    }
 
    // 妫?煡瀛楃涓叉槸鍚︿负绾暟瀛?
    public static boolean isNumeric(String str) {
        for (int i = str.length(); --i >= 0;) {
            if (!Character.isDigit(str.charAt(i))) {
                return false;
            }
        }
        return true;
    }
 
    public static boolean isLetter(String s) {
        for (int i = 0; i < s.length(); i++) {
            if (!(s.charAt(i) >= 'A' && s.charAt(i) <= 'Z')
                    && !(s.charAt(i) >= 'a' && s.charAt(i) <= 'z')) {
                return false;
            }
        }
        return true;
    }
 
    // 鍘婚櫎瀛楃涓蹭腑绌烘牸
    public static String clearSpaces(String aString) {
        StringTokenizer aStringTok = new StringTokenizer(aString, " ", false);
        String aResult = "";
        while (aStringTok.hasMoreElements()) {
            aResult += aStringTok.nextElement();
        }
        return aResult;
    }
 
    /**
     * is null or its length is 0 or it is made by space
     *
     * @param str
     * @return if string is null or its size is 0 or it is made by space, return
     *         true, else return false.
     */
    public static boolean isBlank(String str) {
        return (str == null || str.trim().length() == 0);
    }
 
    /**
     * is null or its length is 0
     *
     * @param str
     * @return if string is null or its size is 0, return true, else return
     *         false.
     */
    public static boolean isEmpty(String str) {
        return (str == null || str.length() == 0);
    }
 
    /**
     * compare two string
     *
     * @param actual
     * @param expected
     * @return
     * @see ObjectUtils#isEquals(Object, Object)
     */
//    public static boolean isEquals(String actual, String expected) {
//        return ObjectUtils.isEquals(actual, expected);
//    }
 
    /**
     * capitalize first letter
     *
     * <pre>
     * capitalizeFirstLetter(null)     =   null;
     * capitalizeFirstLetter("")       =   "";
     * capitalizeFirstLetter("2ab")    =   "2ab"
     * capitalizeFirstLetter("a")      =   "A"
     * capitalizeFirstLetter("ab")     =   "Ab"
     * capitalizeFirstLetter("Abc")    =   "Abc"
     * </pre>
     *
     * @param str
     * @return
     */
    public static String capitalizeFirstLetter(String str) {
        if (isEmpty(str)) {
            return str;
        }
        char c = str.charAt(0);
        return (!Character.isLetter(c) || Character.isUpperCase(c)) ? str
                : new StringBuilder(str.length())
                .append(Character.toUpperCase(c))
                .append(str.substring(1)).toString();
    }
 
 
    public static boolean isInt(String text) {
        if (text == null || text.length() == 0) {
            return false;
        }
        try {
            Integer.parseInt(text);
            return true;
        } catch (Exception e) {
            return false;
        }
    }
 
    public static final String[] imageEndWiths = new String[] { ".jpg", ".gif",
            ".png", ".bmp" };
 
    public static boolean isEmail(String email) {
        String regex = "\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*";
        Pattern p = Pattern.compile(regex);
        Matcher m = p.matcher(email);
        return m.find();
    }
 
    /**
     * 是否是正确的电话号码
     *
     * @param mobile
     * @return
     */
    public static boolean isMobile(String mobile) {
 
        String regex = "^((13[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();
        }
    }
 
    public static boolean isVoice(String text) {
 
        return text.toLowerCase().endsWith(".amr");
 
    }
 
    public static boolean isImage(String text) {
        text = text.toLowerCase();
        for (int i = 0; i < imageEndWiths.length; i++) {
            String endWidth = imageEndWiths[i];
            if (text.endsWith(endWidth)) {
                return true;
            }
        }
        return false;
    }
 
    public static String createNewFileName(String fileName) {
 
        int i = fileName.lastIndexOf(".");
 
        String end = fileName.substring(i, fileName.length());
 
        return System.currentTimeMillis() + end;
 
    }
 
    public static boolean isTrimEmpty(String text) {
        if (text == null || text.trim().length() == 0) {
            return true;
        }
        return false;
    }
 
    public static boolean isNullOrEmpty(String text) {
        if (text == null || text.length() == 0 || text.equals("null")) {
            return true;
        }
        return false;
    }
 
    public static String itrim(String beginTimeDis) {
        if (beginTimeDis != null) {
            int length = beginTimeDis.length();
            StringBuffer buffer = new StringBuffer();
            for (int i = 0; i < length; i++) {
                char c = beginTimeDis.charAt(i);
                if (c != ':') {
                    buffer.append(c);
                } else {
                    buffer.append(":");
                }
            }
            return buffer.toString();
        }
 
        return null;
    }
 
    public static String getLocationDis(double distance) {
        if (distance < 0) {
            return "0m";
        } else if (distance < 100) {
            int d = (int) distance;
            return d + "m";
        } else {
            String desc = "";
 
            double f = distance / 1000d;
 
            DecimalFormat df = new DecimalFormat("#.###");
 
            desc = df.format(f);
 
            return desc + "km";
 
        }
 
    }
 
    public static int toInt(String nextText, int i) {
        try {
            int value = Integer.parseInt(nextText);
            return value;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return i;
    }
 
    /**
     * 身份证验�?
     *
     * @param card
     * @return
     */
    public static boolean isIdCard(String card) {
        Pattern p = Pattern.compile("^(\\d{14}|\\d{17})(\\d|[xX])$");
        Matcher matcher = p.matcher(card);
        return matcher.matches();
    }
 
    public static boolean isCharecter(String str) {
        String regEx = "[`~!@#$%^&*()+=|{}':;',\\[\\].<>/?~!@#�?…�?&*()—�?+|{}【�?‘;:�?“�?。,、?]";
        Pattern p = Pattern.compile(regEx);
        Matcher m = p.matcher(str);
        return m.find();
    }
 
    private static final char HEX_DIGITS[] = { '0', '1', '2', '3', '4', '5',
            '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
 
    private static String toHexString(byte[] b) {
        // String to byte
        StringBuilder sb = new StringBuilder(b.length * 2);
        for (int i = 0; i < b.length; i++) {
            sb.append(HEX_DIGITS[(b[i] & 0xf0) >>> 4]);
            sb.append(HEX_DIGITS[b[i] & 0x0f]);
        }
        return sb.toString();
    }
 
    /*
     * MD5加密
     */
    public static String MD5(String s) {
        try {
            // Create MD5 Hash
            MessageDigest digest = java.security.MessageDigest
                    .getInstance("MD5");
            digest.update(s.getBytes());
            byte messageDigest[] = digest.digest();
 
            return toHexString(messageDigest);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
 
        return "";
    }
 
    public static String getSimpleString(String st) {
        if (isNullOrEmpty(st)) {
            return "暂无";
        }
        return st;
    }
 
    // 判断List是否为空
 
    public static boolean listIsNullOrEmpty(List<String> list) {
        if (list == null || list.size() == 0)
            return true;
        else
            return false;
    }
 
    // 获取手机型号
    public static String PeopleModel() {
        Build bd = new Build();
        String model = bd.MODEL;
        return model;
    };
 
    public static String PeopleDeviceId(Context context) {
        TelephonyManager tm = (TelephonyManager) context
                .getSystemService(Context.TELEPHONY_SERVICE);
        String deviceid ="";
        if (ContextCompat.checkSelfPermission(context, android.Manifest.permission.READ_PHONE_STATE) == PackageManager.PERMISSION_GRANTED
                || ContextCompat.checkSelfPermission(context, android.Manifest.permission.READ_PHONE_STATE) == PackageManager.PERMISSION_GRANTED) {
            deviceid = tm.getDeviceId();// 获取deviceId
        }
 
        return deviceid;
    }
 
    public static void getdiffrentColor(TextView tv, int color, int start,
                                        int end) {
        SpannableStringBuilder style = new SpannableStringBuilder(tv.getText());
        ForegroundColorSpan redSpan = new ForegroundColorSpan(color);
        style.setSpan(redSpan, start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        tv.setText(style);
    }
 
    public static void getdiffrentColor(TextView tv, int color, int size,
                                        int start, int end) {
        SpannableStringBuilder style = new SpannableStringBuilder(tv.getText());
        ForegroundColorSpan redSpan = new ForegroundColorSpan(color);
        style.setSpan(redSpan, start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        RelativeSizeSpan sizeSpan = new RelativeSizeSpan(size);
        style.setSpan(sizeSpan, start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        tv.setText(style);
    }
 
    public static String get2Number(int number) {
        if (number >= 0 && number < 10)
            return "0" + number;
        else
            return number + "";
    }
 
 
}