Java CSV String Split getCsvValues(String line)

Here you can find the source of getCsvValues(String line)

Description

get Csv Values

License

Open Source License

Declaration

public static List<String> getCsvValues(String line) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.util.ArrayList;

import java.util.List;

public class Main {
    public static List<String> getCsvValues(String line) {

        List<String> values = delimitedStringToList(line, ",");

        return values;
    }//from   w w w. ja v a2s  .  co  m

    public static List<String> delimitedStringToList(String value, String delimiter) {

        if (value == null || delimiter == null) {
            return null;
        }

        String[] valuesArray = value.split(delimiter, -1);

        List<String> values = new ArrayList<String>(valuesArray.length);

        for (int index = 0; index < valuesArray.length; index++) {
            String currentValue = valuesArray[index];
            currentValue = hardcoreTrim(currentValue);
            values.add(currentValue);
        }

        return values;
    }

    public static String hardcoreTrim(String value) {

        if (value == null) {
            return null;
        }

        int startOfFirstNonSpaceCharacter = -1;
        int startOfLastNonSpaceCharacter = -1;

        int firstIndex = 0;
        int lastIndex = value.length() - 1;
        boolean keepGoing = firstIndex < lastIndex;

        int valueLength = value.length();

        while (keepGoing) {

            if (startOfFirstNonSpaceCharacter == -1 && firstIndex < valueLength) {
                char firstChar = value.charAt(firstIndex);
                if (!Character.isSpaceChar(firstChar)) {
                    startOfFirstNonSpaceCharacter = firstIndex;
                } else {
                    firstIndex++;
                }
            }

            if (startOfLastNonSpaceCharacter == -1 && lastIndex > 0) {
                char lastChar = value.charAt(lastIndex);
                if (!Character.isSpaceChar(lastChar)) {
                    startOfLastNonSpaceCharacter = lastIndex;
                } else {
                    lastIndex--;
                }
            }

            keepGoing = false;

            if (firstIndex > 0 && firstIndex < valueLength && lastIndex > 0 && lastIndex < valueLength
                    && firstIndex < lastIndex) {
                if (startOfFirstNonSpaceCharacter == -1 || startOfLastNonSpaceCharacter == -1) {
                    keepGoing = true;
                }
            }
        }

        String hardcoreTrimmed = null;
        int hardcoreTrimmedLength = (startOfLastNonSpaceCharacter + 1) - startOfFirstNonSpaceCharacter;
        if (hardcoreTrimmedLength == valueLength) {
            return value;
        }

        if (startOfFirstNonSpaceCharacter != -1 && startOfLastNonSpaceCharacter != -1) {
            hardcoreTrimmed = value.substring(startOfFirstNonSpaceCharacter, startOfLastNonSpaceCharacter + 1);
        }

        return hardcoreTrimmed;
    }
}

Related

  1. csvSplit(String csvLine)
  2. csvToArray(String s)
  3. csvToTrimArray(CharSequence charsequence)
  4. getContentsFromNumpyCSVString( String numpyString)
  5. getValuesFromCSVString(String csvString)
  6. SplitCSV(String csv)
  7. splitCSV(String inputString)
  8. splitCSV(String str)