StringHelper.java 36.5 KB
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 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300
package com.yanzuoguang.util.helper;

import com.yanzuoguang.util.YzgError;
import com.yanzuoguang.util.base.ObjectHelper;
import com.yanzuoguang.util.exception.ExceptionHelper;

import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 字符串帮主类
 *
 * @author 颜佐光
 */
public class StringHelper {
    /**
     * 空字符串常量
     */
    public static final String EMPTY = "";
    public static final String TYPE_FLOAT = "float";
    public static final String TYPE_DOUBLE = "double";
    public static final String TYPE_INT = "int";
    public static final String TYPE_LONG = "long";
    public static final String TYPE_BOOL = "boolean";
    public static final String TYPE_OBJECT = "System.Object";
    public static final String UNDER_FLAG = "_";
    public static final String ID_TAG = "z";

    //------------------------------------------------------ 空值判断和处理  -----------------------------------------------------------------------

    /**
     * 检测字符是否为空
     *
     * @param p 需要检测的字符
     * @return 检测结果
     */
    public static boolean isEmptyChar(char p) {
        return p == '\r'
                || p == '\n'
                || p == '\t'
                || p == ' ';
    }

    /**
     * 判断传入的字符串是否为空
     *
     * @param froms 需要检测的字符串
     * @return 是否为空
     */
    public static boolean isEmpty(Object... froms) {
        for (Object from : froms) {
            boolean isEmpty = from == null || from.toString().length() < 1;
            if (isEmpty) {
                return true;
            }
        }
        return false;
    }

    /**
     * 是否属于空数组
     *
     * @param val
     * @return
     */
    public static boolean isEmptyArray(Object val) {
        if (val == null) {
            return true;
        }
        // 判断是否属于数组
        boolean isArray = ArrayHelper.isArrayOrList(val);
        // 不是数组,则为常规值
        if (!isArray) {
            return false;
        }
        List list = ArrayHelper.getList(val);
        return list.isEmpty();
    }

    /**
     * 判断字符串是否为空
     *
     * @param from 来源数据
     * @return 是否为空
     */
    public static boolean isEmpty(String from) {
        return from == null || from.length() < 1;
    }

    /**
     * 获取空字符串
     *
     * @param str
     * @return
     */
    public static String getEmpty(Object str) {
        if (isEmpty(str)) {
            return "";
        }
        return str.toString();
    }

    /**
     * 取左边
     *
     * @param from
     * @param size
     * @return
     */
    public static String left(String from, int size) {
        if (from != null) {
            return from.substring(0, Math.min(from.length(), size));
        }
        return StringHelper.EMPTY;
    }

    /**
     * 取右边
     *
     * @param from
     * @param size
     * @return
     */
    public static String right(String from, int size) {
        if (from != null) {
            return from.substring(Math.max(from.length() - size, 0));
        }
        return StringHelper.EMPTY;
    }


    /**
     * 传入很多值,返回第一个不等于排除值、空值的值,当全部为排除值时,返回默认值
     *
     * @param expandValue 排除值
     * @param defReturn   默认返回值
     * @param froms       其他值
     * @param <T>         数据类型
     * @return 返回值
     */
    public static <T> T getFirstRun(T expandValue, T defReturn, T... froms) {
        if (froms == null) {
            return defReturn;
        }
        for (T from : froms) {
            if (from == null || isEmpty(from)) {
                continue;
            }
            if (expandValue == null) {
                return from;
            }
            if (expandValue == from || expandValue.equals(from)) {
                continue;
            }
            return from;
        }
        return defReturn;
    }

    /**
     * 传入很多字符串,获取第一个非空的字符串,至少需要两个参数
     *
     * @param froms 参数列表
     * @return 第一个非空字符串
     */
    public static String getFirst(String... froms) {
        return getFirstRun(EMPTY, EMPTY, froms);
    }

    /**
     * 传入很多整形,获取第一个非0的值,至少需要两个参数
     *
     * @param froms 参数列表
     * @return 第一个非0值
     */
    public static Integer getFirst(Integer... froms) {
        return getFirstRun(0, 0, froms);
    }

    /**
     * 传入很多整形,获取第一个非0的值,至少需要两个参数
     *
     * @param froms 参数列表
     * @return 第一个非0值
     */
    public static Long getFirst(Long... froms) {
        return getFirstRun(0L, 0L, froms);
    }

    /**
     * 传入很多整形,获取第一个非0的值,至少需要两个参数
     *
     * @param froms 参数列表
     * @return 第一个非0值
     */
    public static Double getFirst(Double... froms) {
        return getFirstRun(0.0, 0.0, froms);
    }

    /**
     * 传入很多整形,获取第一个非0的值,至少需要两个参数
     *
     * @param froms 参数列表
     * @return 第一个非0值
     */
    public static Float getFirst(Float... froms) {
        return getFirstRun(0.0f, 0.0f, froms);
    }

    /**
     * 获取第一个非空值
     *
     * @param froms 返回值
     * @return 默认返回false
     */
    public static Boolean getFirst(Boolean... froms) {
        return getFirstRun(null, false, froms);
    }

    /**
     * 传入很多字符串,获取第一个非空的字符串,至少需要两个参数
     *
     * @param froms 参数列表
     * @return 第一个非空字符串
     */
    public static String getFirstNull(String... froms) {
        return getFirstRun(EMPTY, null, froms);
    }

    /**
     * 检测值是否为数字
     *
     * @param from 需要检测的值
     * @return 检测结果
     */
    public static boolean isNumber(String from) {
        if (StringHelper.isEmpty(from)) {
            return false;
        }
        final char[] chars = from.toCharArray();
        int sz = chars.length;
        boolean hasExp = false;
        boolean hasDecPoint = false;
        boolean allowSigns = false;
        boolean foundDigit = false;
        // deal with any possible sign up front
        final int start = (chars[0] == '-') ? 1 : 0;
        // leading 0
        if (sz > start + 1 && chars[start] == '0') {
            if (
                    (chars[start + 1] == 'x') ||
                            (chars[start + 1] == 'X')
            ) { // leading 0x/0X
                int i = start + 2;
                if (i == sz) {
                    // str == "0x"
                    return false;
                }
                // checking hex (it can't be anything else)
                for (; i < chars.length; i++) {
                    boolean inputHex = (chars[i] < '0' || chars[i] > '9')
                            && (chars[i] < 'a' || chars[i] > 'f')
                            && (chars[i] < 'A' || chars[i] > 'F');
                    if (inputHex) {
                        return false;
                    }
                }
                return true;
            } else if (Character.isDigit(chars[start + 1])) {
                // leading 0, but not hex, must be octal
                int i = start + 1;
                for (; i < chars.length; i++) {
                    if (chars[i] < '0' || chars[i] > '7') {
                        return false;
                    }
                }
                return true;
            }
        }
        sz--; // don't want to loop to the last char, check it afterwords
        // for type qualifiers
        int i = start;
        // loop to the next to last char or to the last char if we need another digit to
        // make a valid number (e.g. chars[0..5] = "1234E")
        while (i < sz || (i < sz + 1 && allowSigns && !foundDigit)) {
            if (chars[i] >= '0' && chars[i] <= '9') {
                foundDigit = true;
                allowSigns = false;

            } else if (chars[i] == '.') {
                if (hasDecPoint || hasExp) {
                    // two decimal points or dec in exponent
                    return false;
                }
                hasDecPoint = true;
            } else if (chars[i] == 'e' || chars[i] == 'E') {
                // we've already taken care of hex.
                if (hasExp) {
                    // two E's
                    return false;
                }
                if (!foundDigit) {
                    return false;
                }
                hasExp = true;
                allowSigns = true;
            } else if (chars[i] == '+' || chars[i] == '-') {
                if (!allowSigns) {
                    return false;
                }
                allowSigns = false;
                // we need a digit after the E
                foundDigit = false;
            } else {
                return false;
            }
            i++;
        }
        if (i < chars.length) {
            if (chars[i] >= '0' && chars[i] <= '9') {
                // no type qualifier, OK
                return true;
            }
            if (chars[i] == 'e' || chars[i] == 'E') {
                // can't have an E at the last byte
                return false;
            }
            if (chars[i] == '.') {
                if (hasDecPoint || hasExp) {
                    // two decimal points or dec in exponent
                    return false;
                }
                // single trailing decimal point after non-exponent is ok
                return foundDigit;
            }
            boolean allowD = !allowSigns
                    && (chars[i] == 'd'
                    || chars[i] == 'D'
                    || chars[i] == 'f'
                    || chars[i] == 'F');
            if (allowD) {
                return foundDigit;
            }
            if (chars[i] == 'l'
                    || chars[i] == 'L') {
                // not allowing L with an exponent or decimal point
                return foundDigit && !hasExp && !hasDecPoint;
            }
            // last character is illegal
            return false;
        }
        // allowSigns is true iff the val ends in 'E'
        // found digit it to make sure weird stuff like '.' and '1E-' doesn't pass
        return !allowSigns && foundDigit;
    }

    //------------------------------------------------------ 转换值  -----------------------------------------------------------------------

    /**
     * 类型匹配
     *
     * @param type  类型
     * @param vBase 类型的名称
     * @return 读取的结果
     */
    public static boolean isType(Class<?> type, Class<?> vBase) {
        if (type == null) {
            return false;
        } else if (type.equals(vBase)) {
            return true;
        }
        if (TYPE_OBJECT.equals(type.toString())) {
            return false;
        } else {
            return isType(type.getSuperclass(), vBase);
        }
    }

    /**
     * 判断类型是否是整形
     *
     * @param cls
     * @return
     */
    public static boolean isInt(Class<?> cls) {
        String vName = cls.getName();
        return TYPE_INT.equals(vName) || isType(cls, Integer.class);
    }

    /**
     * 根据类型来获取值
     *
     * @param fromCls 类型
     * @param from    对象
     * @return 返回获取的值
     */
    public static <T> T to(Class<T> fromCls, Object from) {
        Object to;
        if (isType(fromCls, String.class)) {
            to = toString(from);
        } else if (isBaseType(TYPE_BOOL, Boolean.class, fromCls, from)) {
            String strValue = toString(from);
            to = toBoolean(strValue);
        } else if (isBaseType(TYPE_INT, Integer.class, fromCls, from)) {
            to = toInt(from);
        } else if (isBaseType(TYPE_LONG, Long.class, fromCls, from)) {
            to = toLong(from);
        } else if (isBaseType(TYPE_DOUBLE, Double.class, fromCls, from)) {
            to = toDouble(from);
        } else if (isBaseType(TYPE_FLOAT, Float.class, fromCls, from)) {
            to = toFloat(from);
        } else if (isType(fromCls, Date.class)) {
            to = DateHelper.getDateTime(from);
        } else if (fromCls.isEnum()) {
            String strValue = toString(from);
            if (strValue != null) {
                to = EnumHelper.toEnum(fromCls, strValue);
            } else {
                to = null;
            }
        } else {
            to = null;
        }
        if (to == null && from == null) {
            return null;
        } else if (to != null) {
            return (T) to;
        } else if (fromCls == List.class && from instanceof List) {
            return (T) from;
        } else if (fromCls == Map.class && from instanceof Map) {
            return (T) from;
        } else if (fromCls == Object.class) {
            return (T) from;
        } else {
            if (ObjectHelper.isSub(fromCls, from.getClass())) {
                return (T) from;
            }
            return (T) to;
        }
    }

    private static boolean isBaseType(String clsName, Class<?> cls, Class<?> fromCls, Object fromValue) {
        if (clsName.equals(fromCls.getName())) {
            return true;
        }
        if (fromValue == null) {
            return false;
        }
        return isType(fromCls, cls);
    }

    /**
     * 将 object 转换为String
     *
     * @param obj 需要转换的object
     * @return 转换成功的字符串
     */
    public static String toString(Object obj) {
        return obj == null ? null : obj.toString();
    }

    /**
     * 将字符串转换为 bool ,当不能转换时,则默认为false
     *
     * @param from     需要转换的字符串
     * @param defValue 默认值
     * @return 转换成功后的值
     */
    public static boolean toBoolean(Object from, boolean defValue) {
        boolean result = defValue;
        try {
            if (!isEmpty(from)) {
                result = (!compare(from, "0", true)
                        && !compare(from, "false", true)
                        && !compare(from, "no", true)
                        && !isEmpty(from));
            }
        } catch (Exception ex) {
            ExceptionHelper.handleException(StringHelper.class, ex, from);
        }
        return result;
    }

    /**
     * 将字符串转换为 bool ,当不能转换时,则默认为false
     *
     * @param from 需要转换的字符串
     * @return 转换成功后的值
     */
    public static boolean toBoolean(Object from) {
        return toBoolean(from, false);
    }

    /**
     * 将字符串转换为 Int,当不能转换时,则默认为0
     *
     * @param from 需要转换的字符串
     * @return 转换成功后的值
     */
    public static short toShort(Object from) {
        short result = 0;
        try {
            if (!isEmpty(from)) {
                result = Short.parseShort(from.toString());
            }
        } catch (Exception ex) {
            ExceptionHelper.handleException(StringHelper.class, ex, from);
        }
        return result;
    }

    /**
     * 将字符串转换为 Int,当不能转换时,则默认为0
     *
     * @param from 需要转换的字符串
     * @param def  转换不成功的默认值
     * @return 转换成功后的值
     */
    public static int toInt(Object from, int def) {
        int result = def;
        try {
            if (!isEmpty(from)) {
                // String类型的小数转int会出错
                result = Integer.parseInt(from.toString());
            }
        } catch (Exception ex) {
            try {
                double d = Double.parseDouble(from.toString());
                int i = (int) d;
                result = Integer.parseInt(String.valueOf(i));
            } catch (Exception e) {
                ExceptionHelper.handleException(StringHelper.class, ex, from);
            }
            ExceptionHelper.handleException(StringHelper.class, ex, from);
        }
        return result;
    }

    /**
     * 将字符串转换为 Int,当不能转换时,则默认为0
     *
     * @param from 需要转换的字符串
     * @return 转换成功后的值
     */
    public static int toInt(Object from) {
        return toInt(from, 0);
    }

    /**
     * 将字符串转换为 long,当不能转换时,则默认为0
     *
     * @param from 需要转换的字符串
     * @return 转换成功后的值
     */
    public static long toLong(Object from) {
        long result = 0;
        try {
            if (!isEmpty(from)) {
                result = Long.parseLong(from.toString());
            }
        } catch (Exception ex) {
            ExceptionHelper.handleException(StringHelper.class, ex, from);
        }
        return result;
    }

    /**
     * 将字符串转换为 long,当不能转换时,则默认为0
     *
     * @param from 需要转换的字节数组
     * @return 转换成功后的值
     */
    public static long toLong(byte[] from) {
        if (from.length > 0) {
            return ByteHelper.toLongByLH(from);
        }
        return 0L;
    }

    /**
     * 将字符串转换为 Float,当不能转换时,则默认为0
     *
     * @param from 需要转换的字符串
     * @return 转换成功后的值
     */
    public static float toFloat(Object from) {
        float result = 0;
        try {
            if (!isEmpty(from)) {
                result = Float.parseFloat(from.toString());
            }
        } catch (Exception ex) {
            ExceptionHelper.handleException(StringHelper.class, ex, from);
        }
        return result;
    }

    /**
     * 将字符串转换为 Double,当不能转换时,则默认为0
     *
     * @param from 需要转换的字符串
     * @return 转换成功后的值
     */
    public static double toDouble(Object from) {
        double result = 0.0;
        try {
            if (!isEmpty(from)) {
                result = Double.parseDouble(from.toString());
            }
        } catch (Exception ex) {
            ExceptionHelper.handleException(StringHelper.class, ex, from);
        }
        return result;
    }

    /**
     * 将字符串转换为Decimal,当不能转换时,则默认值为0
     *
     * @param from 需要转换的字符串
     * @return 转换成功后的值
     */
    public static double toDecimal(Object from) {
        double result = 0;
        try {
            if (!isEmpty(from)) {
                result = Double.valueOf(from.toString());
            }
        } catch (Exception ex) {
            ExceptionHelper.handleException(StringHelper.class, ex, from);
        }
        return result;
    }

    /**
     * 将对象转换为价格,空值用'-'表示
     *
     * @param from 需要转换的价格
     * @return 价格字符串
     */
    public static String toPrice(Object from) {
        String str = "-";
        if (!isEmpty(from) && toDouble(from) != 0) {
            str = "" + toDouble(from);
        }
        return str;
    }

    //------------------------------------------------------ 字符串编码  -----------------------------------------------------------------------

    /**
     * 使用Unicode转码
     *
     * @param str
     * @return
     */
    public static String encoding(String str) {
        return encoding("UTF-8", str);
    }

    /**
     * 字符串编码转换
     *
     * @param charset 输入字符串
     * @param from    输入字符串
     * @return
     */
    public static String encoding(String charset, String from) {
        byte[] buff;
        try {
            buff = from.getBytes(charset);
            // 将字节流转换为字符串
            from = new String(buff, charset);
            from = from.replace("\0", "").replace("\n", "");
        } catch (UnsupportedEncodingException e) {
            ExceptionHelper.handleException(StringHelper.class, e, from);
        }
        return from;
    }
    //------------------------------------------------------ 常用函数  -----------------------------------------------------------------------

    /**
     * 将字符串进行分割
     *
     * @param from  需要分割的字符串
     * @param regex 分割的正则表达式
     * @return 分割后的字符串
     */
    public static String[] split(String from, String regex) {
        return from.split(regex);
    }

    /**
     * 将 list 转换为字符串
     *
     * @param list
     * @return
     */
    public static String join(Object... list) {
        return join(Arrays.asList(list));
    }

    /**
     * 将 list 转换为字符串
     *
     * @param list 需要处理的字符串列表
     * @return 处理后的字符串
     */
    public static String join(List list) {
        StringBuilder sb = new StringBuilder();
        for (Object item : list) {
            if (item == null) {
                continue;
            }
            String to = item.toString();
            if (isEmpty(to)) {
                continue;
            }
            if (sb.length() > 0) {
                sb.append(",");
            }
            sb.append(to);
        }
        return sb.toString();
    }

    /**
     * 对比字符串是否相等,不区分大小写
     *
     * @param from 第一个字符串
     * @param to   第二个字符串
     * @return 相等时返回true
     */
    public static boolean compare(Object from, Object to) {
        return compare(String.valueOf(from), String.valueOf(to), false);
    }

    /**
     * 对比字符串是否相等,不区分大小写
     *
     * @param from 第一个字符串
     * @param to   第二个字符串
     * @return 相等时返回true
     */
    public static boolean compare(String from, String to) {
        return compare(from, to, false);
    }

    /**
     * 比较两个字符串是否相等
     *
     * @param from       字符串1
     * @param to         字符串2
     * @param ignoreCase 是否区分大小写
     * @return 比较结果
     */
    public static boolean compare(String from, String to, boolean ignoreCase) {
        if (from == null && to == null) {
            return true;
        } else if (from == null || to == null) {
            return false;
        } else if (from.length() != to.length()) {
            return false;
        } else if (ignoreCase) {
            return from.compareToIgnoreCase(to) == 0;
        } else {
            return from.compareTo(to) == 0;
        }
    }

    /**
     * 比较两个对象转换为字符串是否相等
     *
     * @param from       字符串1
     * @param to         字符串2
     * @param ignoreCase 是否区分大小写
     * @return 比较结果
     */
    public static boolean compare(Object from, Object to, boolean ignoreCase) {
        String fromStr = toString(from);
        String toStr = toString(to);
        return compare(fromStr, toStr, ignoreCase);
    }

    //------------------------------------------------------ 转换值  -----------------------------------------------------------------------

    /**
     * 计算分页数量
     *
     * @param count 数据条数
     * @param size  每页大小
     * @return
     */
    public static int getPage(int count, int size) {
        if (size != 0) {
            return count / size + (count % size > 0 ? 1 : 0);
        }
        return 0;
    }

    /**
     * 计算分页数量
     *
     * @param count 数据条数
     * @param size  每页大小
     * @return
     */
    public static long getPage(long count, long size) {
        if (size != 0) {
            return count / size + (count % size > 0 ? 1 : 0);
        }
        return 0;
    }

    /**
     * 将字符串转换为菜单
     *
     * @param args 需要获取的路径,会自动剔除空字符串 ["a" ,"b" ]
     * @return 获取到的路径, 结果为 a => b
     */
    public static String getGo(String... args) {
        StringBuilder sb = new StringBuilder();
        for (String arg : args) {
            if (isEmpty(arg)) {
                continue;
            }
            if (sb.length() > 0) {
                sb.append(" => ");
            }
            sb.append(arg);
        }
        return sb.toString();
    }

    /**
     * 判断字符串是否是整数
     */
    public static boolean isInteger(String value) {
        try {
            Integer.parseInt(value);
            return true;
        } catch (NumberFormatException e) {
            return false;
        }
    }

    /***
     * 判断是否在数组中
     * @return
     */
    public static boolean isIn(int from, int... tos) {
        for (int to : tos) {
            if (from == to) {
                return true;
            }
        }
        return false;
    }

    /***
     * 判断是否在数组中
     * @return
     */
    public static boolean isIn(String from, String... tos) {
        return isIn(false, from, tos);
    }

    /***
     * 判断是否在数组中
     * @return
     */
    public static boolean isIn(boolean ignoreCase, String from, String... tos) {
        for (String to : tos) {
            if (StringHelper.compare(from, to, ignoreCase)) {
                return true;
            }
        }
        return false;
    }


    /**
     * 获取UUID
     *
     * @return
     */
    public static String getUUID() {
        return UUID.randomUUID().toString().replace("-", "");
    }

    /**
     * 获取主键
     *
     * @return
     */
    public static String getNewID() {
        String tag = getNewIdTag(System.currentTimeMillis());
        String id = getUUID();
        return tag + id.substring(tag.length());
    }


    /**
     * MD5加密
     *
     * @param string 源字符串
     * @return 加密后的字符串
     */
    public static String md5(String string) {
        try {
            byte[] hash = MessageDigest.getInstance("MD5").digest(string.getBytes(StandardCharsets.UTF_8));
            StringBuilder hex = new StringBuilder(hash.length * 2);
            for (byte b : hash) {
                if ((b & 0xFF) < 0x10) {
                    hex.append("0");
                }
                hex.append(Integer.toHexString(b & 0xFF));
            }
            return hex.toString();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return StringHelper.EMPTY;
    }

    /**
     * MD5加密
     *
     * @param from 需要加密的字符串
     * @return 加密的结果
     */
    public static String md51(String from) {
        String to = from;
        try {
            // 生成实现指定摘要算法的 MessageDigest 对象。
            MessageDigest md = MessageDigest.getInstance("MD5");
            // 使用指定的字节数组更新摘要。
            md.update(from.getBytes(StandardCharsets.UTF_8));
            // 通过执行诸如填充之类的最终操作完成哈希计算。
            byte[] b = md.digest();
            // 生成具体的md5密码到buf数组
            int i;
            StringBuilder buf = new StringBuilder();
            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));
            }
            to = buf.toString();
            // Log.info(class, "32位: " + buf.toString());// 32位的加密
            // Log.info(class, "16位: " + buf.toString().substring(8, 24));// 16位的加密,其实就是32位加密后的截取
        } catch (Exception e) {
            ExceptionHelper.handleException(StringHelper.class, e, from);
        }
        return to;
    }

    /**
     * 获取组合编号的MD5值
     *
     * @param froms 需要组合的编号
     * @return MD5后的值
     */
    public static String getNewMD5Id(Date date, Object... froms) {
        if (date == null) {
            throw YzgError.getRuntimeException("017");
        }
        String tag = getNewIdTag(date.getTime());
        String id = md51(getId(froms));
        return tag + id.substring(tag.length());
    }

    /**
     * 获取新Id标价
     *
     * @param time
     * @return
     */
    private static String getNewIdTag(long time) {
        return String.format("%s%015d", ID_TAG, time);
    }

    /**
     * 获取组合编号的MD5值
     *
     * @param date 需要组合的编号
     * @param args 需要组合的编号
     * @return MD5后的值
     */
    public static String getNewIdMD5(Date date, Object... args) {
        return getNewMD5Id(date, args);
    }

    /**
     * 获取组合编号的MD5值
     *
     * @param froms 需要组合的编号
     * @return MD5后的值
     */
    public static String getMD5Id(Object... froms) {
        return md51(getId(froms));
    }

    /**
     * 获取组合编号的MD5值
     *
     * @param froms 需要组合的编号
     * @return MD5后的值
     */
    public static String getIdMD5(Object... froms) {
        return getMD5Id(froms);
    }


    /**
     * 获取组合编号
     *
     * @param args 需要组合的编号
     * @return 将ID列表进行组合生成ID
     */
    public static String getIdShort(String from, Object... args) {
        if (from == null) {
            return StringHelper.EMPTY;
        }
        String id = getId(args);
        return from.replaceFirst(id + ":", "");
    }

    /**
     * 获取组合编号
     *
     * @param args 需要组合的编号
     * @return 将ID列表进行组合生成ID
     */
    public static String getId(Object... args) {
        StringBuilder sb = new StringBuilder();
        for (Object arg : args) {
            if (sb.length() > 0) {
                sb.append(":");
            }
            sb.append(arg);
        }
        return sb.toString();
    }

    /**
     * 获取组合的SQL语句
     *
     * @param froms SQL语句
     * @return
     */
    public static String getSql(String... froms) {
        StringBuilder sb = new StringBuilder();
        for (String sql : froms) {
            if (isEmpty(sql)) {
                continue;
            }
            if (sb.length() > 0) {
                sb.append(" ");
            }
            sb.append(sql.trim());
        }
        return sb.toString();
    }

    /**
     * 获取CamelCase命名,去掉下划线
     *
     * @param from 原名称
     * @return 修改后的名称
     */
    public static String getCamelCase(String from) {
        StringBuilder result = new StringBuilder();
        String[] a = from.split("_");
        for (String s : a) {
            if (result.length() == 0) {
                result.append(s.substring(0, 1).toLowerCase());
                result.append(s.substring(1));
            } else {
                result.append(s.substring(0, 1).toUpperCase());
                result.append(s.substring(1));
            }
        }
        return result.toString();
    }

    /***
     * 驼峰命名转为下划线命名大写
     *
     * @param from
     *        驼峰命名的字符串
     */
    public static String getUnderLine(String from) {
        if (from.contains(UNDER_FLAG)) {
            return from;
        }
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < from.length(); i++) {
            char c = from.charAt(i);
            if (Character.isUpperCase(c)) {
                sb.append(UNDER_FLAG);
            }
            sb.append(c);
        }
        return sb.toString().toUpperCase();
    }

    /**
     * 讲guid转换为32位长度的guid
     *
     * @param from
     * @return
     */
    public static String guidTo32(String from) {
        if (isEmpty(from)) {
            return from;
        }
        return from.replaceAll("-", "");
    }


    /**
     * 获取字段替换值
     *
     * @param from   来源字符串
     * @param target  值对象
     * @return 处理后的值对象
     */
    public static String getCodeString(String from, Object target) {
        return getCodeStringArray(from, target);
    }


    /**
     * 获取字段替换值
     *
     * @param from    来源字符串
     * @param targets  值对象
     * @return 处理后的值对象
     */
    public static String getCodeStringArray(String from, Object... targets) {
        return getFormat(from, StringHelper.EMPTY, new StringFormatHandle() {
            @Override
            public void addPos(StringBuilder sb, String gorup, String fieldFull, String field, String command) {
                String value = StringHelper.EMPTY;
                for (Object target : targets) {
                    value = StringHelper.getFirst(ObjectHelper.getString(target, field), EMPTY);
                    if (!StringHelper.isEmpty(value)) {
                        break;
                    }
                }
                sb.append(value);
            }
        });
    }

    private static final Pattern reg = Pattern.compile("\\{(.+?)\\}");

    /**
     * 进行字段格式化处理
     *
     * @param format            需要处理得格式字符串
     * @param defaultField      默认字段
     * @param fieldFormatHandle 对字段进行格式化处理
     * @return 处理之后得值
     */
    public static String getFormat(String format, String defaultField, StringFormatHandle fieldFormatHandle) {
        StringBuilder sb = new StringBuilder();
        Matcher matches = reg.matcher(format);
        int start = 0;
        while (matches.find()) {
            int len = matches.start() - start;
            if (len > 0) {
                sb.append(format, start, matches.start());
            }
            String group = matches.group(0);
            String fieldFull = matches.group(1);
            if (fieldFull.startsWith("0") && !StringHelper.isEmpty(defaultField)) {
                fieldFull = defaultField + fieldFull.substring(1);
            }
            String[] fields = fieldFull.split(":");
            String field = fields[0];
            String fieldFormat = StringHelper.EMPTY;
            if (fields.length > 1) {
                fieldFormat = fieldFull.substring(field.length() + 1);
            }
            fieldFormatHandle.addPos(sb, group, fieldFull, field, fieldFormat);
            start = matches.end();
        }
        sb.append(format.substring(start));
        return sb.toString();
    }


    /**
     * 去掉两边的空格
     *
     * @param from 需要去掉空格的字符串
     * @return 去掉字符串的值
     */
    public static String trim(String from) {
        if (from == null) {
            return EMPTY;
        }
        return from.trim();
    }

    /**
     * 去掉两边的空格
     *
     * @param from 需要去掉空格的字符串
     * @return 去掉字符串的值
     */
    public static String trim(String from, String... suffix) {
        if (from == null || from.isEmpty()) {
            return EMPTY;
        }
        return trimRight(trimLeft(from, suffix), suffix);
    }

    /**
     * 去掉左边的字符串
     *
     * @param from
     * @param suffix
     * @return
     */
    public static String trimLeft(String from, String... suffix) {
        if (from == null || from.isEmpty()) {
            return EMPTY;
        }
        boolean handle = true;
        while (handle) {
            handle = false;
            for (String prefix : suffix) {
                if (from.startsWith(prefix)) {
                    from = from.substring(prefix.length());
                    handle = true;
                }
            }
        }
        return from;
    }

    /**
     * 去掉左边的字符串
     *
     * @param from
     * @param suffix
     * @return
     */
    public static String trimRight(String from, String... suffix) {
        if (from == null || from.isEmpty()) {
            return EMPTY;
        }
        boolean handle = true;
        while (handle) {
            handle = false;
            for (String prefix : suffix) {
                if (from.endsWith(prefix)) {
                    from = from.substring(0, from.length() - prefix.length());
                    handle = true;
                }
            }
        }
        return from;
    }

    /**
     * 删除开头字符串
     *
     * @param from
     * @param suffix
     * @return
     */
    public static String trimStart(String from, String... suffix) {
        return trimLeft(from, suffix);
    }

    /**
     * 删除末尾字符串
     *
     * @param from
     * @param suffix
     * @return
     */
    public static String trimEnd(String from, String suffix) {
        return trimRight(from, suffix);
    }

}