Java Comma Separated List toListOfStringsDelimitedByCommaOrSemicolon(String s)

Here you can find the source of toListOfStringsDelimitedByCommaOrSemicolon(String s)

Description

This version returns the complete list, including empty string, if such entry is empty

License

Open Source License

Parameter

Parameter Description
s input string delimited by comma or semicolon

Return

list of strings where deliminators are stripped off. if no content is found between 2 delimitors then empty string is returned in its place

Declaration

public static List toListOfStringsDelimitedByCommaOrSemicolon(String s) 

Method Source Code


//package com.java2s;

import java.util.*;

public class Main {
    /**//  w w  w  . j av  a2  s .  co m
     * Define split characters *
     */
    private final static String SEPARATOR = "[,;]";

    /**
     * This version returns the complete list, including empty string, if such entry is empty
     *
     * @param s input string delimited by comma or semicolon
     * @return list of strings where deliminators are stripped off. if no content
     * is found between 2 delimitors then empty string is returned in its place
     */
    public static List toListOfStringsDelimitedByCommaOrSemicolon(String s) {
        if (s == null) {
            return Collections.EMPTY_LIST;
        }

        List results = new ArrayList();
        String[] terms = s.split(SEPARATOR);

        for (int i = 0; i < terms.length; i++) {
            //this has empty string if nothing is found
            String term = terms[i].trim();
            results.add(term);
        }

        return results;
    }
}

Related

  1. toCommaSeparated(List list)
  2. toCommaSeparatedString(List list)
  3. toList(final String commaSeparatedString)
  4. toListDelimitedByComma(String s)
  5. toListOfNonEmptyStringsDelimitedByCommaOrSemicolon(String s)