把以逗号分隔的字符串转换成list

时间:2022-12-04 19:35:44
/**
     * 把省的字符串转换成列表
     * 
     * @param province
     * @return
     */
    private List<String> getProvinceList(String province) {
        List<String> result = new ArrayList<>();
        if (StringUtils.isEmpty(province)) {
            return result;
        }
        int index = 0;
        int currIndex = province.indexOf(Constants.COMMA, index);
        if (currIndex > -1) {
            while (currIndex > -1 || index < province.length()) {
                String str = "";
                if (currIndex < 0) {
                    str = province.substring(index, province.length());
                    currIndex = index + str.length();
                } else {
                    str = province.substring(index, currIndex);
                }
                if (StringUtils.isNoneBlank(str)) {
                    result.add(str);
                }
                index = ++currIndex;
                currIndex = province.indexOf(Constants.COMMA, index);
            }
        } else {
            result.add(province);
        }
        return result;
    }