0

I have the following String (based on a CSV):

str = '234,23,"sdf,23", "sdf", 23-APR-21, "sd",,'

I only want to get the element at the 5th position. So normally I just do str.split(",")[4] => '23-APR-21'

But now I get returned str.split(",")[4] => '"sdf"'

Since applying just "," on the escaped part of the CSV returns already two parts. "sdf,23" => sdf and 23

How would you solve this without using a special CSV parser?

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
John Smith
  • 6,105
  • 16
  • 58
  • 109
  • not sure if i understand, can you provide a few sample srings along with the expected result for each? – szaman Feb 22 '22 at 20:33
  • 5
    "*without using a special CSV parser?*" Like trying to drive a nail without a hammer, why wouldn't you use a tool *specifically designed for CSV data*...? Why reinvent the wheel and needlessly require accounting for all the edge cases, etc. you might encounter when something robust and well-tested has already been built *for your use*? You don't even need a "*special*" one; any spec-compliant one will do just fine. – esqew Feb 22 '22 at 20:36
  • I think you mis counted. try using str.split(",")[5] – FujiRoyale Feb 22 '22 at 20:38
  • But I think you might be looking for a more robust solution. – FujiRoyale Feb 22 '22 at 20:38
  • You could use regexp to firt change commas that are betwen ” ”, into something else, then split. – jamomani Feb 22 '22 at 20:44
  • @esqew ... Of cause it's possible without a (full-blown) csv parser. One just needs to combine a `split`ting regex like [`/("[^"]+"|\s*,\s*)/`](https://regex101.com/r/Au4jvB/1) with a reduce task. Such an approach is also reliable for the OP's use case of empty CSV positions/slots, unlike the highly rated approaches of the above duplicate link which pretends referring to correct answers without taking into account the OP's special use case. – Peter Seliger Feb 22 '22 at 21:37
  • solution ... `'234,23, "sdf,23,2323" , "sdf" , 23-APR-21 , , 345, "sd" , , '.split(/("[^"]+"|\s*,\s*)/).reduce((result, token, idx, arr) => { token = token.trim(); if ( token === ',' && arr[idx + 1] === '' && String(arr[idx + 2]).trim() === ',' ) { result.push(undefined); } else if ( (token !== '') && (token !== ',') ) { result.push(token); } return result; }, []);` – Peter Seliger Feb 22 '22 at 21:39
  • 1
    @PeterSeliger Not sure where I said or even implied that it's *not* possible - mind pointing out where I may have so I can correct? Just like it *is* possible to drive a nail without a hammer, it most certainly is possible to roll your own CSV parser. My question was *why*? When questions like this come up here, 90% of the time there is really no good underlying reason or there's some misunderstanding that this type of requirement stems from - trying to get to the bottom of it with clarifying questions as this one usually helps the OP to understand where their understanding has become flawed. – esqew Feb 22 '22 at 21:54
  • @esqew ... agreed, with all your points/arguments. – Peter Seliger Feb 23 '22 at 00:33

0 Answers0