Java List Trim stringToListTrim(String str, char delimiter)

Here you can find the source of stringToListTrim(String str, char delimiter)

Description

creates a List from a String "List", trims empty values at start and end

License

LGPL

Parameter

Parameter Description
str String to creates List from
delimiter delimiter to split string

Return

List

Declaration

public static List stringToListTrim(String str, char delimiter) 

Method Source Code


//package com.java2s;
//License from project: LGPL 

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

public class Main {
    /**//from  ww  w  .  ja  v  a  2  s. co m
     * creates a List from a String "List", trims empty values at start and end
     * @param str String to creates List from
     * @param delimiter delimiter to split string
     * @return List 
     */
    public static List stringToListTrim(String str, char delimiter) {
        ArrayList list = new ArrayList();
        int len = str.length();
        if (len == 0)
            return list;
        StringBuffer el = new StringBuffer();
        boolean hasStart = false;

        for (int i = 0; i < len; i++) {
            char c = str.charAt(i);
            if (c == delimiter) {
                if (!hasStart) {
                    if (el.length() > 0) {
                        list.add(el.toString());
                        hasStart = true;
                    }
                } else {
                    list.add(el.toString());
                }
                if (el.length() > 0)
                    el = new StringBuffer();
            } else {
                el.append(c);
            }
        }
        if (el.length() > 0)
            list.add(el.toString());

        // remove empty items on the end
        for (int i = list.size() - 1; i >= 0; i--) {
            if (list.get(i).toString().length() == 0) {
                list.remove(i);
            } else
                break;
        }

        return list;
    }
}

Related

  1. addTrimString(List aStringList, String aString)
  2. formatSampleText(final List lines, final int maxTrim)
  3. getTrimmedList(List listA)
  4. splitAndTrimCSVToList(String csv)
  5. splitAsList(String source, char delimiter, boolean trim)
  6. toDelimitedString(List values, String delimiter, boolean trimValues)
  7. tokenizeAndTrimToList(String value, String delimiter)
  8. toList(final String line, final boolean trimSpaces)
  9. toStringList(List list, String delimiter, boolean isTrim, boolean isRemoveEmpty)