Android String to CSV Convert parseFromCSV(String str)

Here you can find the source of parseFromCSV(String str)

Description

parse From CSV

Declaration

public static String[] parseFromCSV(String str) 

Method Source Code

//package com.java2s;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main {
    public static String[] parseFromCSV(String str) {
        Pattern csvPattern = Pattern
                .compile("\"([^\"]*)\"|(?<=,|^)([^,]*)(?:,|$)");
        Matcher matcher = csvPattern.matcher(str);
        List<String> allMatches = new ArrayList<String>();

        while (matcher.find()) {
            String match = matcher.group(1);
            if (match != null) {
                allMatches.add(match);/*from w ww . j  av  a 2  s.  c  om*/
            } else {
                allMatches.add(matcher.group(2));
            }
        }
        int size = allMatches.size();
        if (size > 0) {
            return (String[]) allMatches.toArray(new String[size]);
        }

        return new String[0];
    }
}