Here you can find the source of stringToListTrim(String str, char delimiter)
Parameter | Description |
---|---|
str | String to creates List from |
delimiter | delimiter to split string |
public static List stringToListTrim(String str, char delimiter)
//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; } }