Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Decomposing a large string into small parts without damaging word parts

I have a large array that I need to send in small sections (between 200 characters and 400 characters)

The problem that arises for me is that I don’t want to cut the words but only in the spaces

How can I create from the string a division into small sections?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

To give an example I used a text generator

Unbowed, Unbent, Unbroken. It's ten thousand miles between Kings landing and the wall. The tourney of Ashford Meadows. Pay the iron price. The winds of Winter. King in the North. The War of the 5 kings. It's ten thousand miles between Kings landing and the wall. The winds of Winter. Words are like wind. Bastards are born of passion, aren't they? We don't despise them in Dorne. The night is dark and full of terrors. And now his watch is ended. May the Father judge him justly. The rains of castamere. What is dead may never die. A Lannister always pays his debts. Never Resting. A good act does not wash out the bad, nor a bad act the good. Each should have its own reward. Unbowed, Unbent, Unbroken.

I want the result to be Unbowed, Unbent, Unbroken. It's ten thousand and not Unbowed, Unbent, Unbroken. It's ten thou

Thank you

>Solution :

var string = 'Unbowed, Unbent, Unbroken. It\'s ten thousand miles between Kings landing and the wall. The tourney of Ashford Meadows. Pay the iron price. The winds of Winter. King in the North. The War of the 5 kings. It\'s ten thousand miles between Kings landing and the wall. The winds of Winter. Words are like wind. Bastards are born of passion, aren\'t they? We don\'t despise them in Dorne. The night is dark and full of terrors. And now his watch is ended. May the Father judge him justly. The rains of castamere. What is dead may never die. A Lannister always pays his debts. Never Resting. A good act does not wash out the bad, nor a bad act the good. Each should have its own reward. Unbowed, Unbent, Unbroken.'

var temporaryString = '';
var words = string.split(' ');
var temporaryArray = [];
var dividedext = [];
for (var i = 0; i < words.length; i++) {
  temporaryArray.push(words[i]);
  if (temporaryArray.join(' ').length > 200) {
    temporaryString = temporaryArray.pop();
    dividedext.push(temporaryArray.join(" "));
    if (i == words.length - 1) {
      dividedext.push(temporaryString);
    } else {
      temporaryArray = [temporaryString];
    }
  }
}
dividedext.push(temporaryArray.join(' '))
console.log(dividedext);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading