Java String to List toList(String val)

Here you can find the source of toList(String val)

Description

to List

License

Open Source License

Declaration

private static List<String> toList(String val) 

Method Source Code

//package com.java2s;
/**/*from   w w  w  . j  a  v  a2s. c o m*/
 * Copyright (c) 2015, Lucee Assosication Switzerland. All rights reserved.
 *
 * 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; either 
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * 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.
 * 
 * You should have received a copy of the GNU Lesser General Public 
 * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
 * 
 */

import java.util.ArrayList;

import java.util.List;

public class Main {
    private static List<String> toList(String val) {
        List<String> list = new ArrayList<String>();
        int len = val.length();
        int inside = 0;
        char c;
        int begin = 0;
        for (int i = 0; i < len; i++) {
            c = val.charAt(i);
            if (c == '"') {
                if (inside == '"')
                    inside = 0;
                else if (inside == 0)
                    inside = '"';
            } else if (c == '\'') {
                if (inside == '\'')
                    inside = 0;
                else if (inside == 0)
                    inside = '\'';
            } else if (c == ',' && inside == 0) {
                if (begin < i)
                    list.add(val.substring(begin, i));
                begin = i + 1;
            }
        }
        if (begin < len)
            list.add(val.substring(begin));

        return list;
    }

    private static void add(StringBuilder sb, String name, String value, String defaultValue) {
        if (value == null) {
            if (defaultValue == null)
                return;
            value = defaultValue;
        }
        sb.append(name).append(": ").append(value).append('\n');
    }
}

Related

  1. toList(String str)
  2. toList(String str)
  3. toList(String string)
  4. toList(String text)
  5. toList(String transaction)
  6. toList(String val, String separator)