1

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.

akshay Tiwari
  • 137
  • 3
  • 11
  • 3
    "I tried a lot of ways" doesn't actually describe your effort. Share some details, please. – Scott Sauyet Nov 07 '22 at 16:52
  • 2
    what is your attempt? Stack Overflow is not the place to ask a coding problem and expect answers when you haven't shown what you've tried – Samathingamajig Nov 07 '22 at 16:52
  • so something like splitting a string to chunks? split the string by empty space and then do this https://stackoverflow.com/questions/8495687/split-array-into-chunks – cmgchess Nov 07 '22 at 16:55
  • Also, either your question is confused or your arithmetic is off. Shouldn't `<500 words>` be broken into`[, , , and ]`, which has four groups? – Scott Sauyet Nov 07 '22 at 16:56
  • so split it into words and splice off 128, 128 and put the rest in the last array.... – epascarello Nov 07 '22 at 16:58

3 Answers3

2

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))
jessica-98
  • 609
  • 1
  • 6
0

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)
akshay Tiwari
  • 137
  • 3
  • 11
0

Since there are already working answers here, I will share my recursive approach. But in the future, please share the work you've done yourself. If you haven't been able to code anything, then share some of your thought-processes.

Here we build this atop a reusable function, chunk that splits any array into equal-sized chunks, plus a possibly smaller final chunk. Our chunkWords function splits a string at white space, then runs chunk on the result.

const chunk = (n) => (xs) =>
  xs .length <= n ? [xs] : [xs .slice (0, n), ... chunk (n) (xs .slice (n))]

const chunkWords = (n) => (s) =>
  chunk (n) (s .split (/\s+/)) // .map (ss => ss .join (' '))

console .log (
  chunkWords (3) ('Now is the time for all good men to come to the aid of their party')
)

If you want the result to be turned back into strings, just uncomment the .map call in chunkWords. That would yield ["Now is the", "time for all", "good men to", "come to the", "aid of their", "party"].

Scott Sauyet
  • 49,207
  • 4
  • 49
  • 103