Creates a list from the line with the given separator character. - Java java.util

Java examples for java.util:List Creation

Description

Creates a list from the line with the given separator character.

Demo Code

/**/*from w  w  w  . j a va2  s  .  c o  m*/
 * Helpful methods for collections.
 * 
 * ## Legal stuff
 * 
 * Copyright 2014-2014 Ekkart Kleinod <ekleinod@edgesoft.de>
 * 
 * This file is part of edgeUtils.
 * 
 * edgeUtils is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * edgeUtils is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with edgeUtils.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * @author Ekkart Kleinod
 * @version 0.2
 * @since 0.2
 */
//package com.java2s;
import java.util.ArrayList;
import java.util.Arrays;

import java.util.List;

public class Main {
    public static void main(String[] argv) {
        String theLine = "java2s.com";
        String theSeparator = "java2s.com";
        System.out.println(fromCSVString(theLine, theSeparator));
    }

    /**
     * Creates a list from the line with the given separator character.
     * 
     * The method returns a list with as many entries as are expected,
     * i.e. it fills the list with empty strings if needed.
     * 
     * @param theLine line to split
     * @param theSeparator separator between entries
     * @return list with splitted, trimmed entries
     * 
     * @version 0.2
     * @since 0.2
     */
    public static List<String> fromCSVString(String theLine,
            String theSeparator) {

        List<String> lstReturn = new ArrayList<>();
        String sSeparator = (theSeparator.equals(".")) ? "\\."
                : theSeparator;

        for (String theEntry : Arrays.asList(theLine.split(sSeparator))) {
            lstReturn.add(theEntry.trim());
        }

        int iSepCount = countOccurrences(theLine, theSeparator.charAt(0));
        for (int i = lstReturn.size(); (i <= iSepCount); i++) {
            lstReturn.add("");
        }

        return lstReturn;
    }

    /**
     * Returns the number of occurrences of needle in haystack.
     * 
     * @param haystack string to search through
     * @param needle character to find
     * @return number of occurences of needle in haystack
     * 
     * @version 0.2
     * @since 0.2
     */
    public static int countOccurrences(String haystack, char needle) {
        int iCount = 0;
        for (char c : haystack.toCharArray()) {
            if (c == needle) {
                iCount++;
            }
        }
        return iCount;
    }
}

Related Tutorials