Get Last Part Of CSV String
Say I have a CSV string: red,yellow,green,blue How would I programatically select blue from the string using jQuery? The data is returned via an AJAX request from a PHP script ren
Solution 1:
Solution 2:
Or even
var csv_data = text.substr(text.lastIndexOf(",") + 1);
Solution 3:
No jQuery, plain JavaScript:
var csv_Data = text.split(',');
var last = csv_Data[csv_Data.length - 1];
I strongly recommend against making synchronous calls.
Reference: string.split
Update: If you really only want to get the last value, you can use lastIndexOf
and
substr
:
var last = text.substr(text.lastIndexOf(',') + 1);
Solution 4:
You could use the jQuery CSV plugin as per instructions here. However, CSV format usually has quotes around the values. It would be easier to send the data in JSON format from the PHP file using json_encode().
Post a Comment for "Get Last Part Of CSV String"