Home > Software engineering >  break string at specific word count
break string at specific word count

Time:11-08

I have a long string and I want to break that long string into 128 words of pair of arrays. for example. a string has 500 words. then the final output should be an array of 3 element

element has the first 128 words, the second element has the next 128 words, and the third element should have the rest of the words.

I have tried a lot of ways but didn't work for me. please help me out if anyone knows it

thanks in advance.

CodePudding user response:

If you want each array to contain 128 words and not characters, you can first split the string by whiteSpace to store the words in an array and then split it into chunks like this:

const string = "hello world hello world hello world "
const words = string.split(" ")


function sliceIntoChunks(arr, chunkSize) {
  const noEmptyStrsArr = arr.filter(item => item.length > 0)

  const res = [];
  for (let i = 0; i < noEmptyStrsArr.length; i  = chunkSize) {
    const chunk = noEmptyStrsArr.slice(i, i   chunkSize);
    res.push(chunk);
  }
  return res;
}

console.log(sliceIntoChunks(words, 2))

CodePudding user response:

thanks, @jessica-98 for your solution. I have added a few more lines if someone wants 128 words as a string not as separate words. then you can consider this solution.

const string = "Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of \"de Finibus Bonorum et Malorum\" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum, \"Lorem ipsum dolor sit amet..\", comes from a line in section 1.10.32.Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of \"de Finibus Bonorum et Malorum\" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum,\"Lorem ipsum dolor sit amet..\", comes from a line in section 1.10.32.Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of  "
const words = string.split(" ")


function sliceIntoChunks(arr, chunkSize) {
  const noEmptyStrsArr = arr.filter(item => item.length > 0)

  const res = [];
  for (let i = 0; i < noEmptyStrsArr.length; i  = chunkSize) {
    const chunk = noEmptyStrsArr.slice(i, i   chunkSize);
    res.push(chunk);
  }
  return res;
}

var res=sliceIntoChunks(words, 128)

var newArr=[]
for(let i=0;i<res.length;i  ){
/* for(let j=0;j<res[i].length;j  ){
newArr.push()
}*/

newArr.push(res[i].join(" "))

}

console.log(newArr)

CodePudding user response:

You can do it much Shorter way by using reduce(), slice() and flatMap():

const longStr = [...Array(500)].reduce((acc, curr) => acc ? `${acc} random` : "random", ''); //Generate a long string with 500 words

const words = longStr.split(" ");
const ans = [...Array(Math.ceil(words.length/128))].reduce((acc, _, i)=> [...acc, words.slice(i*128, (i*128) 128)],[]).flatMap((subArr) => subArr.join(" "));

console.log(ans);

  • Related