【问题标题】:Split the string based on given delimeter ignoring double quted part根据给定的分隔符拆分字符串,忽略双引号部分
【发布时间】:2015-12-05 14:59:38
【问题描述】:

我需要根据分隔符将字符串拆分为子字符串。 这是一个代码:

String[] fields = line.split(Pattern.quote(CSV_DELIMITER)); //CSV_DELIMITER=","

我得到以下行 "1234, "dfd, ddss", 35345" 预期结果将有 3 个字段:1234、“dfd、ddss”、35345,但它有 4:1234、dfd、ddss、35345 我该如何解决? (双引号内的内容不要拆分)

【问题讨论】:

    标签: java string csv split delimiter


    【解决方案1】:

    使用 Apache Commons 的 CSVParser。

    它可以处理包含分隔符的字段(以及更多):

    https://commons.apache.org/proper/commons-csv/

    CSVFormat.withDelimiter(',').withQuote('"')
    

    CSVFormat.EXCEL 将是一个快捷方式:

     CSVFormat
     .withDelimiter(',')
     .withQuote('"')
     .withRecordSeparator("\r\n")
     .withIgnoreEmptyLines(false)
     .withAllowMissingColumnNames(true)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-09-05
      • 1970-01-01
      • 2018-10-26
      • 1970-01-01
      • 1970-01-01
      • 2010-12-17
      相关资源
      最近更新 更多