Skip to content
Advertisement

Count no. of delimiters in line while ignoring the delimiter which is part of data value

I am reading one big csv file line by line and I want to count the no. of delimiters in each line.

But if the delimiter is part of data value, then it should not be counted.

Few records of data set:

JavaScript

In all 3 lines, number of actual commas (which divides the data into multiple columns) are only 2

but below code snippet outputs

  • 4 commas for line 1
  • 2 for line 2
  • 3 for line 3

Code Snippet:

JavaScript

Advertisement

Answer

One simple way could be to use regex and remove everything between two ", so that the commas inside aren’t counted.

JavaScript

Output:

JavaScript
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement