Red
Red

Reputation: 7299

How to split this string into chunks

I have a string which looks like this (subtitle file):

"1\n00:00:27,560 --> 00:00:29,990\nHandelingen 19:5\n\"En toen zij dit hoorden",

I want it to become an array like this (with a length of 3):

var array = [
    "1",
    "00:00:27,560 --> 00:00:29,990",
    "Handelingen 19:5 \"En toen zij dit hoorden"
]

This is what Ive tried, but I did not get any further than this.

// I putted \n in to act as the linebreaks.
var string = "1\n00:00:27,560 --> 00:00:29,990\nHandelingen 19:5\n\"En toen zij dit hoorden,";

// I did not get any further than this :/
var chunks = string.split('\n');

console.log(chunks);

How can I split the first two lines and let the lines after the first two join each other. And what is the fastest / most efficient way to do it? The amount of paragraphs can grow to 2500.

Upvotes: 0

Views: 191

Answers (4)

Red
Red

Reputation: 7299

With the help of the anwsers given by @Tyblitz and @JaredT I managed to resolve it. Using .slice() and .join()

// I putted \n in to act as the linebreaks.
var string = "1\n00:00:27,560 --> 00:00:29,990\nHandelingen 19:5\n\"En toen zij dit hoorden,";

// I did not get any further than this :/
var chunks = string.split('\n');
var array = [];

array.push(
  chunks.slice(0, 1).join(), 
  chunks.slice(1, 2).join(), 
  chunks.slice(2, chunks.length).join()
);

console.log(array);

Upvotes: 0

Jared Teng
Jared Teng

Reputation: 311

Is this what you mean? There is probably a better way of doing it, but this should work.

   var string = "1\n00:00:27,560 --> 00:00:29,990\nHandelingen 19:5\n\"En toenzij dit hoorden,";

    var chunks = string.split('\n', 2);
    chunks[2] = string.substr(chunks[0].length+chunks[1].length+2,string.length);
//.replace(/\n/, ""); optional

    console.log(chunks[0]);
    console.log(chunks[1]);
    console.log(chunks[2]);

Upvotes: 1

webketje
webketje

Reputation: 10976

I happened to have written an SRT sub file parser a while ago. Run the code snippet to see the result, the function you are interested in are parseSub and parseSubs

function parseSub(sub) {
  sub = sub.split(/\r*\n/);
  
  var line1 = sub[0],
      line2 = sub[1].split(/\s*-->\s*/),
      start = line2[0],
      end   = line2[1],
      text  = sub.slice(2).join('');
  
  return {
	  index: parseInt(line1),
    from : start,
    to   : end,
    text : text
  };
}

function parseSubs(fileText) {
  return fileText.trim().split(/\r*\n\s+/).map(function(subtext) {
    return parseSub(subtext);
  });
}


var subsText = document.getElementById('subs')
subsText.textContent = JSON.stringify(parseSubs(subsText.textContent), null, 2);
<pre id="subs">1
00:00:00,800 --> 00:00:04,620
Mr. De Wever, je vous rends la parole dans un instant. J'écoute d'abord Mr. Smet.

2
00:00:04,620 --> 00:00:09,220
Vous l'avez entendu: la médiocrité, un amalgame 'd'unité', 

3
00:00:09,220 --> 00:00:14,340
tout doit être chouette. Je peux quelque part comprendre la préoccupation de la N-VA.

4
00:00:14,340 --> 00:00:16,000
Oh mais je ne comprends pas seulement l'inquiétude de la N-VA,
</pre>

Upvotes: 1

Ussaid Iqbal
Ussaid Iqbal

Reputation: 796

Is it standard string like the string is going to have same kind of data all the time? If yes why don't you split it and then combine the last two elements of array together and store in a variable and then remove the last 2 elements at index 2, 3 then add the variable to the array.

// I putted \n in to act as the linebreaks.
var string = "1\n00:00:27,560 --> 00:00:29,990\nHandelingen 19:5\n\"En toen zij dit hoorden,";

// I did not get any further than this :/




const [id, timestamp, whatever, whatever2] = string.split("\n");
var array = [
   	id,
timestamp,
whatever+whatever2
]
console.log(array);

Upvotes: 0

Related Questions