集册 Java实例教程 编译CSV行

编译CSV行

欢马劈雪     最近更新时间:2020-01-02 10:19:05

392
编译CSV行
/* 来 自 nowjava - 时  代  Java*/


//package com.nowjava;


public class Main {

    static String compileCSVLine(String[] fields, int i, String prefix) {


        String line = (prefix == null) ? "" : prefix;

        for (; i < fields.length; i++) {

            if (line.length() > 0)

                line = line + ",";

            String f = fields[i];


            if (f.startsWith("\"") && f.endsWith("\"")) {

                f = f.substring(1, f.lastIndexOf('"'));

                f = f.replaceAll("\"", "'");

                f = '"' + f + '"';

            } else {
            /** 
            来 自 
            时 代 J a v a - nowjava.com
            **/

                f = f.replaceAll("\"", "'");

            }


            f = f.replaceAll(",", " ");


            if (f.contains(",") && !f.startsWith(
展开阅读全文