1

I have an array that contains dialogue strings of varying length.

I need to split elements that have more than 200 characters into two separate elements. I would need to do the split so that no words are cut in half (but instead split based on whitespace).

Edit: This question is different from simply splitting a string based on white space because it involves inserting and removing elements in the array in the right locations.

Example:

[

"Hello there I'm shorter than 200 characters so do nothing with me",

"I'm longer than 200 characters though so I should be split into two consecutive elements so that my first half stays where I am currently located in the array and my second half goes to the very next position in the array.",

"I'm shorter than 200 so I just stay here as the last element."

]

Desired result:


[

"Hello there I'm shorter than 200 characters so do nothing with me",

"I'm longer than 200 characters though so I should be split into two consecutive elements so that my first",

"half stays where I am currently located in the array and my second half goes to the very next position in the array.",

"I'm shorter than 200 so I just stay here as the last element."

]

Failed attempt (I have no idea really):


const myArray = [

"Hello there I'm shorter than 200 characters so do nothing with me",

"I'm longer than 200 characters though so I should be split into two consecutive elements so that my first half stays where I am currently located in the array and my second half goes to the very next position in the array.",

"I'm shorter than 200 so I just stay here as the last element."

]

myArray.forEach((element) => {
  if (element.length > 200){

let split = element.substring(0,element.length/2);
myArray.push(split);

}
});


user44109
  • 121
  • 6
  • Post the JavaScript that you've tried and failed with. See [ask] and how to post a [mcve]. – zer00ne Aug 10 '22 at 06:43
  • Does the string have to be split in half or can it be split anywhere as long as the maximum length is < 200? – Nick Aug 10 '22 at 06:57
  • What if there's a string which is (for example) 500 characters long? What is your desired split result in that case? – Nick Aug 10 '22 at 07:00
  • Check my answer posted on the original question. https://stackoverflow.com/a/73301963/12183892 – Shubham Ambastha Aug 10 '22 at 07:02
  • For example: `myArray.map(s => s.match(/.{1,200}(\s|$)/g)).flat()` – Nick Aug 10 '22 at 07:10
  • Or perhaps: `myArray.map(s => s.length <= 200 ? s : s.match(new RegExp(\`.{1,${s.length/2+10}}(\s|$)\`, 'g'))).flat()` – Nick Aug 10 '22 at 07:13
  • @Nick I would like to split the string roughly in half so that both elements contain roughly the same amount of text. Doesn't have to be perfectly even. If the string is 500 characters long then I would like to split it into three elements. – user44109 Aug 10 '22 at 07:20
  • @ShubhamAmbastha Doesn't .flat() produce a nested array? I would like to have just a single array with no nesting.. – user44109 Aug 10 '22 at 07:22
  • @user44109 my "Or perhaps" code should work for that. `.flat()` flattens a nested array. – Nick Aug 10 '22 at 07:39
  • @user44109 `.flat()` doesn't produce nested array, you'll get nested array after splitting string into parts, so `flat()` will flattens nested array. Have you even run that code? – Shubham Ambastha Aug 11 '22 at 08:34
  • @user44109 I did run that code and got a really weird result which I didn't really understand so I finally abandoned the whole approach and programmed something completely different. – user44109 Aug 12 '22 at 11:09

0 Answers0