Java String to List convertStringToList(String string, String delimiter, boolean trim)

Here you can find the source of convertStringToList(String string, String delimiter, boolean trim)

Description

Converts string to list.

License

Apache License

Parameter

Parameter Description
string String to convert to list
delimiter Delimiter of list elements
trim Whether or not to trim tokens befor putting them in list

Return

List

Declaration

public static List convertStringToList(String string, String delimiter, boolean trim) 

Method Source Code

//package com.java2s;
/*//www. j a va 2s  .  c  om
 *  Copyright 2004 Blandware (http://www.blandware.com)
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

import java.util.*;

public class Main {
    /**
     * Converts string to list. The string is assumed to be a sequence of some
      * elements separated with delimiter.
     *
     * @param string    String to convert to list
     * @param delimiter Delimiter of list elements
     * @param trim      Whether or not to trim tokens befor putting them in list
     * @return List
     */
    public static List convertStringToList(String string, String delimiter, boolean trim) {
        if (string == null || string.length() == 0) {
            return new LinkedList();
        }
        String[] members = string.split(delimiter);
        List list = Collections.synchronizedList(new LinkedList());
        for (int i = 0; i < members.length; i++) {
            String member = members[i];
            if (trim) {
                member = member.trim();
            }
            list.add(member);
        }
        return list;
    }
}

Related

  1. convertStringToEnumList(Class enumType, List stringList)
  2. convertStringToList(String str)
  3. convertStringToList(String value, String delimiter)
  4. convertTextToStringList(String tagText)
  5. convertTextToStringList(String text)
  6. convertToList(String inputString)