Java XML Parse String parse(final String line)

Here you can find the source of parse(final String line)

Description

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

License

Apache License

Parameter

Parameter Description
line The CSV line of text

Return

An array of Strings

Declaration

public static String[] parse(final String line) 

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.
     *
     * @param line
     *            The CSV line of text
     * @return An array of Strings
     */
    public static String[] parse(final String line) {
        return parse(line, false);
    }

    /**
     * Parses a single line (String) of comma-separated values.
     *
     * @param line
     *            The CSV line of text
     * @param trimSpaces
     *            whether to trim any leading/trailing spaces
     * @return An array of Strings
     */
    public static String[] parse(final String line, final boolean trimSpaces) {
        final List<String> list = toList(line, trimSpaces);
        return list.toArray(new String[list.size()]);
    }

    /**
     * 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. parse(final String content)
  2. parse(final String xmlContent)
  3. parse(String input)
  4. parse(String input, char delim, char esc)
  5. parse(String line)