集册 Java实例教程 分析CSV行

分析CSV行

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

446
解析CSV行


//package com.nowjava;/**n o w j a v a . c o m**/


public class Main {

    public static String[] parseCSVLine(String line) {

        String[] field = new String[12];

        String temp = "";

        int index = 0;

        boolean hasQuotation = false;

        for (int i = 0; i < line.length(); i++) {

            Character currentChar = line.charAt(i);

            if (currentChar.equals('\"')) {

                hasQuotation = !hasQuotation;

            }

            if (currentChar.equals(',')) {
            /** from 
            时   代     Java  公  众  号 - nowjava.com**/

                if (!hasQuotation) {

                    field[index] = temp;

                    temp = "";

                    index = index + 1;

                } else {

                    temp = temp + line.charAt(i);

                }

            } else {

                temp = temp + line.charAt(i);

            }

        
展开阅读全文