How To Deal With Commas In Csv Using Javascript
Solution 1:
The proper way to escape separators inside fields is to enclose the fields in double quotes:
Id,Description
123,"Monitor, Samsung"
But then quotes inside the field have to be escaped with another quote:
Id,Description
123,"Monitor 24"", Samsung"
Of course, a different delimiter like ;
could be used, but then you'd still have to check if those appear in the data.
Solution 2:
I tried to with the below code and it worked for me
item.OrgDescriptionString.replace(/\"/g, "\"\"");
I tried to escape single quotes in the string and it worked.
Solution 3:
This is how I have achieved the reported functionality.
In brief: Requirement was to fetch the comma-separated string as it is in the CSV Export.
Assuming that we have a comma-separated string which needs to be exported in the CSV as it is.
For example String value which comes from the data source as:
'StackOverflow', 'Awesome! work by StackOverflow team, cheers to them.'
Answer :
['stackoverflow', 'Awesome! work by stackoverflow team, cheers to them.'].map(string =>string === null ? '' : `\"${string}\"`);
Explanation: So if we quote the comma-separated values and try exporting than the CSV will not consider the comma(,) as a field separator and due to which in CSV export it won't split into multiple columns with respect to numbers of available commas. And the content will appear in the same column.
Ref wiki link: Comma-separated_values
Hope this helps. Cheers!!
Solution 4:
Use double-quotes enclosed string and escape double-quotes in string:
csvData.push( "\\"" + strData.replace( /\\"/g , "\\"\\"" ) +"\\"" )
Solution 5:
One of the probable solution could be to read the file and then replace the read data with ""
Something like this:
readData = readData.replace(/,/g, "");
Post a Comment for "How To Deal With Commas In Csv Using Javascript"