【问题标题】:How to split string by character with exceptions (Java)如何按字符拆分字符串(Java)
【发布时间】:2015-04-23 08:31:36
【问题描述】:

我想处理一个 CSV 文件。 但是处理起来有一定的问题:

我需要将每行拆分; 通常我会使用 .split 方法 btu 在这种情况下有例外:

一行可以有空的“单元格”,所以它看起来像x;y;z;;a;;;b 我仍然需要在数组中获取空​​的。 例如

array[0] = "x";
array[1] = "y";
array[2] = "z";
array[3] = "";

等等。 另一个例外是: 有一个单元格包含 html 代码(其中有“;”)。 因此,如果 ; 字符串不应该被拆分。在。。。之间 ””。 有办法处理吗?

【问题讨论】:

    标签: java arrays string csv split


    【解决方案1】:

    您可以尝试使用 api OpenCSV。这是做同样事情的小例子,

    public class OpenCSVExample {
    
    public static void main(String[] args)
    {
        CSVReader reader = null;
        try
        {
            //Get the CSVReader instance with specifying the delimiter to be used
            reader = new CSVReader(new FileReader("SampleCSVFile.csv"),';');
            String [] nextLine;
            //Read one line at a time
            while ((nextLine = reader.readNext()) != null)
            {
                for(String token : nextLine)
                {
                    //Print all tokens
                    System.out.println(token);
                }
            }
        }
        catch (Exception e) {
            e.printStackTrace();
        }
        finally {
            try {
                reader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    }
    

    以上示例取自此参考ParseCSVFiles,查看更多详细信息。

    【讨论】:

      【解决方案2】:

      解析这种形式的数据是一个常见的问题,已经被 CSV 解析器解决了。您可以使用Apache Commons CSV 并将分隔符更改为; 而不是默认的,

      【讨论】:

        【解决方案3】:

        你可以用 reg exp 得到它。

        public void regExpSeparateWithSemicolon() {
            Scanner scanner = null;
            String[] result = null;
            String testString = null;
            String regularExpression = "(?!=\",\");";
            int counter = 0;
            try {
                scanner = new Scanner(
                        new File("/home/domain/immo/Documents/SampleCsv.csv"));//Path to csv file
                while(scanner.hasNext()) {
                    //String testString = "x;y;z;;a;\";\";b";
                    testString = scanner.next();
                    testString = testString.replaceAll("\";\"","\",\"");                                
                    result = testString.split(regularExpression);
                    for(int index = 0; index < result.length; index++) {
                        System.out.println("result["+counter+++"] = "+
                                result[index].replace(",", ";"));
                    }                   
                }
            } catch (FileNotFoundException fnf) {
                System.out.println("Exception occured :"+fnf);
            } catch (Exception e) {
                System.out.println("Exception occured :"+e);
            } finally {
                if(null != scanner) {
                    scanner.close();    
                }               
            }
        }
        

        SampleCsv.csv

        x;y;z;;a;;;";";b

        1;2;3;;4;;;";";5


        O/P


        result[0] = x
        result[1] = y
        result[2] = z
        result[3] = 
        result[4] = a
        result[5] = 
        result[6] = 
        result[7] = ";"
        result[8] = b
        result[9] = 1
        result[10] = 2
        result[11] = 3
        result[12] = 
        result[13] = 4
        result[14] = 
        result[15] = 
        result[16] = ";"
        result[17] = 5
        

        如果没有 replaceAll,我无法弄清楚如何使它工作。

        希望有人能找到。

        【讨论】:

          猜你喜欢
          • 2012-03-05
          • 1970-01-01
          • 2014-05-27
          • 2012-04-20
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-10-09
          相关资源
          最近更新 更多