I am java script newbie and i have a text file like this Address.txt :
Andhra Pradesh,East Godavari,Reach within 36 Hrs
Andhra Pradesh,Guntur,Reach within 36 Hrs
Andhra Pradesh,Krishna,Reach within 36 Hrs
Andhra Pradesh,Visakhapatnam,Reach within 36 Hrs
Andhra Pradesh,Chittoor,Reach within 36 Hrs
Now i want to separate each loine by substring by comma , hence there will be 3 sub strings. Which must be stored in three arrays.
How to do that in Javascript lets say my way reading this file is :
$.ajax({
type: 'GET',
url: 'Address.txt',
dataType: 'text',
}).success(function (test)
{
alert('inside ajax : '+test);//lets say this show aall the data of test file
var col1 = [];
var col2 = [];
var col3 = [];
var j = 0;
//How to concert them in substring and save in these tree columns ?
for (var i = 0; i <= test.length - 3; i = i + 3)
{
}
})
Use
str.split()
So first we need to split on the newline
\n
character to get all of the lines. Then for each line, we split on the comma,
character to get each of the three substrings, as you say.Here is a verbose but simple way to do it:
So for every line, we add the first substring to the
col1
array, the second to thecol2
array, etc.