简体   繁体   English

具有两个定界符的csv

[英]csv with two delimiters

I am trying to read a csv with two delimiters, which has the following format: 我正在尝试读取具有两个定界符的csv,其格式如下:

    Payment Date,Amount,Member No/branchno
    2018/01/25,58,294416/0

the first part is the date and the last column is the column I am facing issues with. 第一部分是日期,最后一栏是我面临的问题。 I need to split that last column into two columns after the slash. 我需要将斜杠后的最后一列分为两列。

my problem is that i do not know how to separate the last column without affecting the first column, any Help is really appreciated. 我的问题是我不知道如何在不影响第一列的情况下分隔最后一列,因此非常感谢任何帮助。

I can already read through the csv and split the commas. 我已经可以阅读csv并分割逗号。 here is the code for reading through the csv: 这是用于读取csv的代码:

public ArrayList<String[]> ReadCSVFile(File DataFile)
    {    
        try(BufferedReader reader = new BufferedReader (new FileReader(DataFile));){ 

         //while loop to read through the data, while bufferedreader is not null-do .... 
                 while(reader.readLine()!= null)
                {
                    String read = reader.readLine();//bufferedreader string variable
                    String[] OneRow = read.split(","||"/");
                    rs2.add(OneRow);
                    System.out.println(Arrays.toString(OneRow));
        //            
                }
            //BufferedReader to Read through CSV Contents
                  reader.close();

    }//end try 
        catch(Exception ex){
        String errmsg = ex.getMessage();
        //System.out.println("File not Found: "+errmsg);
    }//end exception handling

You can do something like this; 您可以执行以下操作;

while (reader.readLine() != null) {
    String read = reader.readLine();// bufferedreader string variable
    String[] rawRow = read.split(",");
    String lastEntry = rawRow[rawRow.length - 1]; // this contains Member No/branchno
    String[] properLastEntry = lastEntry.split("/"); // this contains Member No, branchno
    String[] oneRow = new String[rawRow.length + 1];
    System.arraycopy(rawRow, 0, oneRow, 0, rawRow.length - 1);
    System.arraycopy(properLastEntry, 0, oneRow, oneRow.length - 2, 2);
}

If you don't want to hard code the length of 2 in the properLastEntry array, you can do something like this instead 如果您不想在properLastEntry数组中硬编码长度为2代码,则可以执行以下操作

while (reader.readLine() != null) {
    String read = reader.readLine();// bufferedreader string variable
    String[] rawRow = read.split(",");
    String lastEntry = rawRow[rawRow.length - 1]; // this contains Member No/branchno
    String[] properLastEntry = lastEntry.split("/"); // this contains Member No, branchno as entries

    // create a memory which can contain rawRow and properLastEntry in a single
    // array
    String[] oneRow = new String[rawRow.length - 1 + properLastEntry.length];
    // copy the data for the finalRow
    System.arraycopy(rawRow, 0, oneRow, 0, rawRow.length - 1);
    System.arraycopy(properLastEntry, 0, oneRow, oneRow.length - properLastEntry.length,
            properLastEntry.length);
}

You want to split string based on two delimiters so you can mention multiple delimiters as follows 您要基于两个定界符来分割字符串,因此可以如下提及多个定界符

split(",|/")

Please go through the below link. 请通过以下链接。 Use string split() with multiple delimiters 将字符串split()与多个定界符一起使用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM