Split a string ignoring quoted sections

前端 未结 13 2406
别跟我提以往
别跟我提以往 2020-12-06 00:15

Given a string like this:

a,\"string, with\",various,\"values, and some\",quoted

What is a good algorithm to split this based on

13条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-06 00:43

    What if an odd number of quotes appear in the original string?

    This looks uncannily like CSV parsing, which has some peculiarities to handling quoted fields. The field is only escaped if the field is delimited with double quotations, so:

    field1, "field2, field3", field4, "field5, field6" field7

    becomes

    field1

    field2, field3

    field4

    "field5

    field6" field7

    Notice if it doesn't both start and end with a quotation, then it's not a quoted field and the double quotes are simply treated as double quotes.

    Insedently my code that someone linked to doesn't actually handle this correctly, if I recall correctly.

提交回复
热议问题