Java List to String toStringList(Scanner scanner, String toSplit, final char separator)

Here you can find the source of toStringList(Scanner scanner, String toSplit, final char separator)

Description

to String List

License

Open Source License

Parameter

Parameter Description
toSplit a parameter
separator a parameter

Declaration

static List<String> toStringList(Scanner scanner, String toSplit, final char separator) 

Method Source Code


//package com.java2s;
/*/*from ww  w  . j ava2s  .c  o m*/
 *    Geotoolkit - An Open Source Java GIS Toolkit
 *    http://www.geotoolkit.org
 *
 *    (C) 2010-2015, Geomatys
 *
 *    This library 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;
 *    version 2.1 of the License.
 *
 *    This library 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.
 */

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;

public class Main {
    /**
     *
     * @param toSplit
     * @param separator
     * @return
     */
    static List<String> toStringList(Scanner scanner, String toSplit, final char separator) {
        if (toSplit == null) {
            return Collections.emptyList();
        }
        final List<String> strings = new ArrayList<>();
        int last = 0;
        toSplit = toSplit.trim();

        String currentValue = null;
        boolean inEscape = false;
        for (;;) {
            if (inEscape) {

                int end = toSplit.indexOf('\"', last);
                while (end >= 0) {
                    if (end >= toSplit.length()) {
                        //found escape end
                        break;
                    } else if (toSplit.charAt(end + 1) == '\"') {
                        //double quote, not an escape
                        end = toSplit.indexOf('\"', end + 2);
                    } else {
                        break;
                    }
                }

                if (end >= 0) {
                    currentValue += toSplit.substring(last, end);
                    currentValue = currentValue.replace("\"\"", "\"");
                    strings.add(currentValue);
                    last = end + 2;
                    inEscape = false;
                } else {
                    currentValue += toSplit.substring(last);
                    currentValue += "\n";
                    last = 0;
                    toSplit = scanner.nextLine();
                }

            } else if (last >= toSplit.length()) {
                break;
            } else if (toSplit.charAt(last) == '\"') {
                inEscape = true;
                last++;
                currentValue = "";
            } else {
                final int end = toSplit.indexOf(separator, last);
                if (end >= 0) {
                    strings.add(toSplit.substring(last, end).trim());
                    last = end + 1;
                } else {
                    strings.add(toSplit.substring(last).trim());
                    break;
                }
            }
        }
        return strings;
    }
}

Related

  1. toStringList(List source)
  2. toStringList(List intList)
  3. toStringList(List l)
  4. toStringList(List as)
  5. toStringList(Map stringMap, String delim)
  6. toStringReplaceBrackets(List list)
  7. toStringResult(List list)
  8. toStringTab(List lineList)

  9. HOME | Copyright © www.java2s.com 2016