1

My csv Data looking like this

1,2,"hi,how ar you?",abc
3,5,a,b,c
expected output should be
4
5
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • Here is what you should find for : http://stackoverflow.com/questions/2952805/count-number-of-lines-in-csv-with-javascript – Manish Kumar Aug 02 '13 at 05:02

2 Answers2

1
'1,2,"hi,how ar you?",abc'.match(/"[^"]*"|[^,]+/g).length
4
'3,5,a,b,c'.match(/"[^"]*"|[^,]+/g).length
5
YOU
  • 120,166
  • 34
  • 186
  • 219
0

Try this method which is derived from another post here:

   var csvLines = '1,2,"how ar you?",abc' + "\n" + '1,4,"fine",6,7';
   var splitByChars = ',';

   var totalCount = 0;
   var linesArray = csvLines.split("\n");

   var lineCount = 0;

   while (lineCount < linesArray.length) {
       totalCount += StringCount(csvLines, splitByChars);
       lineCount++;
   }

   alert(totalCount);

   function StringCount(stringToSplit, splitBy) {
       var words = stringToSplit.split(splitBy);
       return words.length;
   }
mesterak
  • 41
  • 4
  • Here the link where the source code came from: http://stackoverflow.com/questions/13893634/string-split-and-count-the-number-of-occurrences-and-also – mesterak Aug 02 '13 at 05:06
  • I think he just wants to know the number of columns in each row, not the count of occurrences of each word. And it doesn't look like you do anything to keep from treating `"hi,how ar you?"` as 2 columns. – Barmar Aug 02 '13 at 05:30
  • i am expecting the same answer as Barmer – user2450833 Aug 02 '13 at 05:36