So I have a text file with lines of information, with several data pieces on each line that I need to store in an array of arrays.
This is simple enough when they are split on whitespace or mas, but with this particular file, some of the data has white space within a single data field.
Eg.
123 4325 Hello World 43
394 3892 How are you 23
Anybody know a way to split each line into 4 strings each, with 'Hello World' and 'How are you' remaining together?
Ie: Array[0] = [123, 4325, Hello World, 43] and Array[1] = [394, 3892, How are you, 23]
Sorry I'm fairly new to JS so I'm not sure if there's a reall simple answer staring me in the face.
Thanks
So I have a text file with lines of information, with several data pieces on each line that I need to store in an array of arrays.
This is simple enough when they are split on whitespace or mas, but with this particular file, some of the data has white space within a single data field.
Eg.
123 4325 Hello World 43
394 3892 How are you 23
Anybody know a way to split each line into 4 strings each, with 'Hello World' and 'How are you' remaining together?
Ie: Array[0] = [123, 4325, Hello World, 43] and Array[1] = [394, 3892, How are you, 23]
Sorry I'm fairly new to JS so I'm not sure if there's a reall simple answer staring me in the face.
Thanks
Share Improve this question asked Apr 22, 2015 at 6:05 Leon SmithLeon Smith 514 silver badges9 bronze badges 3- So you want to split a string after x-amount of words without it cutting into words? If so, I have asked a question just like this here: stackoverflow./questions/26507116/… – NewToJS Commented Apr 22, 2015 at 6:08
- If this is a clearcut case of single field that has multiple spaces then we can write a custom function. – Sunny R Gupta Commented Apr 22, 2015 at 6:21
- F1 F2 ANYTHING * GOES F3 - is this the structure? – Sunny R Gupta Commented Apr 22, 2015 at 6:22
3 Answers
Reset to default 2Follow a similar workflow.
- Pick a line, split by space.
e.g. "345 578 This is a text 585" -> ["345","578","This", "is", "a", "text","585"]
- Take the first two elements into an array.
e.g. ["345","578","This", "is", "a", "text","585"] -> ["345","578"] ["This", "is", "a", "text","585"]
- Remove the last element into a separate arr.
e.g. ["345","578"] ["This", "is", "a", "text"] ["585"]
- Merge the middle array with space.
e.g. ["345","578"] ["This is a text"] ["585"]
- Merge all arrays.
e.g. ["345","578", "This is a text", "585"]
tada!
You can use the JavaScript API called FileReader. You can search for it in Google. It can read a file using only JS. You can then use
var lines = content.split("\n");
To separate the content by a new line.
While it is not plex, what you want also does not e out of the javascript box. Which means that there are hundreds of ways to resolve this. I remend you choose depending on "soft" requirements.
How fast does the code need to be? How easy should it be to understand the code? Do you also need to make sure about the format of each value (ie that the first value is a number and the third is a string)?
If you need to check for the formatting (which I find is most of the time), I would remend using regular expressions. There are plenty of examples and it would be a different question for SO.
As a solution to the specific problem you have mentioned here, here is the solution I prefer:
function breakByPosition(str, positions) {
var start = 0;
var result = [];
while (positions.length) {
// splice returns the removed values as an array
// It also modified the source array in place
var position = positions.splice(0, 1);
var substring = str.substring(start, position);
// The OP wants by position but apparently not the spaces
// Could be replaced by if (substring !== ' ') depending on needs
if (substring.trim() !== '') {
result.push(substring);
}
start = position;
}
return result;
}
breakByPosition('123 4325 Hello World 43', [3, 4, 8, 9, 20, 21, 23]);
This returns:
["123", "4325", "Hello World", "43"]