简体   繁体   中英

Java simple line parser

I could see bunch of java parsers like OpenCSV, antlr, jsapar etc, but I dont see any of those with ability to specify both custom line seperator and column seperator? Do we have any such easy to use libraries. I dont want to write one using Scanner or Stringtokenizer now!

Eg. A | B || C | D || E | F

want to break this above string to something like {{A,B},{C,D},{E,F}}

You can parse it yourself, it's quite simple to achieve. I haven't test this code practically, you may try it yourself.

line_delimiter = "||";
column_delimiter = "|";

String rows[];
rows = str.split(line_delimiter);
for (int i = 0; i < rows.length; i++) {
    String columns[];
    columns = rows[i].split(column_delimiter);
    for (int j = 0; j < columns.length; j++) {
        // Do something to your data here;
    }
}

Actually, with JSaPar you can have any character sequence for both line delimiter as well as cell delimiter. You specify which delimiter to use within your schema and it can be any number of characters.

The problem you will face by using the same character in both is that the parser will not know if you have a line break or if it is just an empty cell.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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