Java String to List stringToList(String keywords, String delimiter)

Here you can find the source of stringToList(String keywords, String delimiter)

Description

string To List

License

Open Source License

Declaration

public static List<String> stringToList(String keywords,
            String delimiter) 

Method Source Code

//package com.java2s;
/* (c) 2014 Open Source Geospatial Foundation - all rights reserved
 * (c) 2001 - 2013 OpenPlans/*from  w  w  w  .  java2s . c o  m*/
 * This code is licensed under the GPL 2.0 license, available at the root
 * application directory.
 */

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class Main {
    public static List<String> stringToList(String keywords,
            String delimiter) {
        ////
        //
        // In the following cases we return an empty string:
        // - empty or null keyword
        // - empty or null delimiter
        // -delimiter not found at all
        //
        /////
        if (keywords == null || keywords.length() == 0 || delimiter == null
                || delimiter.length() == 0
                | keywords.indexOf(delimiter) < 0)
            return Collections.emptyList();

        ////
        //
        // We know that the delimiter is used at least once, let's spli this string and create the corresponding list.
        //
        /////
        final List<String> elements = new ArrayList<String>();
        int index = -1;
        while ((index = keywords.indexOf(delimiter)) >= 0) {
            if (index > 0)
                elements.add(keywords.substring(0, index));
            keywords = keywords.substring(index);
        }
        if (keywords.length() > 0)
            elements.add(keywords);
        return elements;
    }
}

Related

  1. stringToList(final String string)
  2. stringToList(String content)
  3. stringToList(String content, String splitter)
  4. stringToList(String delimitedString)
  5. stringToList(String input)
  6. stringToList(String listString, String delimiter)
  7. stringToList(String listText)
  8. StringToList(String listText, String regex)
  9. stringToList(String s)