Split a string ignoring quoted sections

前端 未结 13 2375
别跟我提以往
别跟我提以往 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条回答
  •  隐瞒了意图╮
    2020-12-06 00:34

    Of course using a CSV parser is better but just for the fun of it you could:

    Loop on the string letter by letter.
        If current_letter == quote : 
            toggle inside_quote variable.
        Else if (current_letter ==comma and not inside_quote) : 
            push current_word into array and clear current_word.
        Else 
            append the current_letter to current_word
    When the loop is done push the current_word into array 
    

提交回复
热议问题