Java CSV from toCSVString(List values, boolean[] quoteColumn, String fieldSeparator, String quotationChar)

Here you can find the source of toCSVString(List values, boolean[] quoteColumn, String fieldSeparator, String quotationChar)

Description

to CSV String

License

Open Source License

Declaration

public static String toCSVString(List<String> values, boolean[] quoteColumn, String fieldSeparator,
            String quotationChar) 

Method Source Code

//package com.java2s;
/* /*from  w w w .j av  a  2  s. co m*/
 * Maui, Maltcms User Interface. 
 * Copyright (C) 2008-2014, The authors of Maui. All rights reserved.
 *
 * Project website: http://maltcms.sf.net
 *
 * Maui may be used under the terms of either the
 *
 * GNU Lesser General Public License (LGPL)
 * http://www.gnu.org/licenses/lgpl.html
 *
 * or the
 *
 * Eclipse Public License (EPL)
 * http://www.eclipse.org/org/documents/epl-v10.php
 *
 * As a user/recipient of Maui, you may choose which license to receive the code 
 * under. Certain files or entire directories may not be covered by this 
 * dual license, but are subject to licenses compatible to both LGPL and EPL.
 * License exceptions are explicitly declared in all relevant files or in a 
 * LICENSE file in the relevant directories.
 *
 * Maui 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. Please consult the relevant license documentation
 * for details.
 */

import java.util.List;

public class Main {
    public static String toCSVString(List<String> values, boolean[] quoteColumn, String fieldSeparator,
            String quotationChar) {
        StringBuilder sb = new StringBuilder();
        if (values.size() != quoteColumn.length) {
            throw new IllegalArgumentException("values and quoteColumn must have the same lengths!");
        }
        int valsize = values.size();
        for (int i = 0; i < valsize; i++) {
            String s = values.get(i);
            if (quoteColumn[i]) {
                sb.append(quotationChar).append(s).append(quotationChar);
            } else {
                sb.append(s);
            }
            if (i < values.size() - 1) {
                sb.append(fieldSeparator);
            }
        }
        return sb.toString();
    }
}

Related

  1. toCSV(Set list)
  2. toCSVLine(String[] strArray)
  3. toCSVString(List list, boolean quote)
  4. toCsvString(List values)
  5. toCsvString(List values)
  6. toObjectList(String csvString)