,

Java – Parse Integer and String Lists from Text


public static List getIdsList(String ids) {
    ids = StringUtils.stripStart(ids, "{");
    ids = StringUtils.stripEnd(ids, "}");
    List list = parseInts(ids, ',');
    return list;
}

public static List parseInts(String source, char delimeter) {
    if (StringUtils.isEmpty(source)) {
        return Collections.emptyList();
    }
    List result = new ArrayList();
    for (String intStr : source.split(String.valueOf(delimeter))) {
         result.add(NumberUtils.toInt(intStr));
    }
    return result;
}

public static List parseStrings(String source, char delimiter) {
    Pattern p = (delimiter == ',') ? ',' : Pattern.compile(String.valueOf(delimiter));
    return Arrays.asList(p.split(source, 0));
}


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *