Java List Trim toList(final String line, final boolean trimSpaces)

Here you can find the source of toList(final String line, final boolean trimSpaces)

Description

Parses a single line (String) of comma-separated values to a List .

License

Apache License

Parameter

Parameter Description
line The CSV line of text
trimSpaces whether to trim any leading/trailing spaces

Return

a of Strings.

Declaration

public static List<String> toList(final String line, final boolean trimSpaces) 

Method Source Code

//package com.java2s;
/**/*w  w w .  j  a  v  a  2 s  .  c o  m*/
 * Copyright (c) 2008-2009 Acuity Technologies, Inc.
 *
 * 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.
 *
 * Created Feb 18, 2008
 */

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

public class Main {
    /**
     * Parses a single line (String) of comma-separated values to a {@link List}
     * .
     *
     * @param line
     *            The CSV line of text
     * @param trimSpaces
     *            whether to trim any leading/trailing spaces
     * @return a {@link List} of Strings.
     */
    public static List<String> toList(final String line, final boolean trimSpaces) {
        final ArrayList<String> results = new ArrayList<String>();

        int i = 0;
        final int length = line.length();
        while (i < length) {
            final StringBuilder sb = new StringBuilder();
            if (trimSpaces) {
                while (i < length && Character.isWhitespace(line.charAt(i))) {
                    ++i;
                }
            }
            while (i < length && line.charAt(i) != ',') {
                sb.append(line.charAt(i));
                ++i;
            }
            // at this point, should be a comma
            ++i;
            final String s = trimIfNeeded(sb, trimSpaces);
            results.add(s);
        }

        return results;
    }

    /**
     * @param sb
     *            the {@link StringBuilder}
     * @param trimSpaces
     *            true if we should trim spaces from the string we're building
     * @return sb.toString(), trimmed if trimSpaces is true
     */
    private static String trimIfNeeded(final StringBuilder sb, final boolean trimSpaces) {
        final String s;
        if (trimSpaces) {
            s = sb.toString().trim();
        } else {
            s = sb.toString();
        }
        return s;
    }
}

Related

  1. splitAndTrimCSVToList(String csv)
  2. splitAsList(String source, char delimiter, boolean trim)
  3. stringToListTrim(String str, char delimiter)
  4. toDelimitedString(List values, String delimiter, boolean trimValues)
  5. tokenizeAndTrimToList(String value, String delimiter)
  6. toStringList(List list, String delimiter, boolean isTrim, boolean isRemoveEmpty)
  7. trim(final List row)
  8. trim(List data, boolean removeEmptyLines)
  9. trim(List strs)