Parse a CSV Line
Split one line of a CSV export into its fields. Fields are separated by commas. A field may be wrapped in double quotes, in which case it can contain commas, and a doubled quote "" inside it stands for one literal ". The surrounding quotes are not part of the value.
Unquoted fields are taken exactly as written, spaces included. An empty line is one empty field, and a trailing comma produces a final empty field.
The line is well formed: a quote only opens at the very start of a field, and a closing quote is always followed by a comma or the end of the line.
Example 1
Input: line = "a,b,c" Output: ["a","b","c"] Explanation: The quoted name keeps its comma; `""` becomes a single quote.
Example 2
Input: line = "\"Smith, John\",42,\"He said \"\"hi\"\"\"" Output: ["Smith, John","42","He said \"hi\""]
Constraints
- 0 <= line.length <= 10^5
- The line contains no newline characters
Hint 1
Walk the string with an index instead of using split(",").
Hint 2
Inside a quoted field, look one character ahead when you see a quote.
Good practice for: Data Engineer, Backend Developer, Full Stack Developer, Data Scientist, Automation Test Engineer, Database Administrator
Run checks your code on 3 sample tests. Submit runs all 9 tests, including hidden ones, and earns points. Use console.log to debug. Python's first run can take a few seconds while it starts.