Home > Software engineering >  In Javascript, split a string of words into an array of strings of grouped words, grouping for every
In Javascript, split a string of words into an array of strings of grouped words, grouping for every

Time:11-10

How can I split a string into segments of n characters? is similar but not exactly what we are trying to achieve:

// let splitSentence = (sentence, nChar) => {} // creating this function
let sentence = 'the quick cucumber fox jumps over the lazy dog.'
splitSentence(sentence, 15)

// output we are trying to get
// ['the quick cucumber', 'fox jumps over', 'the lazy dog.']

We cannot string split only on spaces, since multiple words are needed in each array element, not just a single word. The process, for nChar == 15, 15 letters are counted into the string, which gets to the quick cucum. The function should finish the word and add the quick cucumber into the output array as first element, then continue counting from the remainder of the sentence, starting at the next word fox, and repeat this process. Not sure if .match() with start and end indexes works for our problem like it does for the SO post linked above.

Putting this logic into splitSentence(). So far we have:

let splitSentence = (sentence, nChar) => {
    let output = [];
    let i = 0;
    while (i < sentence.length) {
        let partOfSentence = sentence.slice(i, i   15);
        output.push(partOfSentence)
        i = i   15
    }
    return output;
} 

However, getting ['the quick cucum', 'ber fox jumps o', 'ver the lazy do', 'g.'], which is not what is needed. Not sure how to keep the entire word together in the slice when we loop over the string.

CodePudding user response:

You can use the following approach:

  • Split the sentence into single characters
  • Loop over the characters: for every character decide:
    • Is it still part of the current character group? This is the case if we have less then the maximum amount of nChar character is the group until now or if we have already overstepped that limit but are still not at the end of the word (= the character we're processing is not a word separator like in your example space)
    • If it is not the part of the current character group we need to create a new group, increase the group count and reset the character count.

As a result of the above process we will get an array of character arrays (string[][]), so we now need to loop over all our groups and join the individual characters of a character group back together using join().

/**
 * 
 * @param {string} sentence 
 * @param {number} nChar 
 * @returns 
 */
const splitSentence = (sentence, nChar) => {
  const chars = sentence.split("");
  let charCount = 0;
  let groupCount = 0;
  const characterGroups = chars.reduce((groups, char) => {
    // if we don't have enough characters or we still have a word to process add the character to the group
    if(charCount < nChar || char != " ") groups[groupCount].push(char)
    else {
      // we have reached the max. number of character and we also are at the end of word
      // => we need to create a new group
      groups.push([])
      // increase group count so we now add characters to a new group
      groupCount  ;
      // reset character count
      charCount = 0;
    }
    charCount  ;
    return groups;
  }, [[]]);
  return characterGroups.map(group => group.join(""));
} 

const sentence = 'the quick cucumber fox jumps over the lazy dog.'
console.log(splitSentence(sentence, 15));
/* StackOverflow snippet: console should overlap rendered HTML area */
.as-console-wrapper { max-height: 100% !important; top: 0; }

Addition

If you want to include the spaces in your output you will only need to change one line:

// instead of
groups.push([])
// use
groups.push([char])

Here the same code with the change so you can see how it affects the result:

/**
 * 
 * @param {string} sentence 
 * @param {number} nChar 
 * @returns 
 */
const splitSentence = (sentence, nChar) => {
  const chars = sentence.split("");
  let charCount = 0;
  let groupCount = 0;
  const characterGroups = chars.reduce((groups, char) => {
    // if we don't have enough characters or we still have a word to process add the character to the group
    if(charCount < nChar || char != " ") groups[groupCount].push(char)
    else {
      // we have reached the max. number of character and we also are at the end of word
      // => we need to create a new group
      groups.push([char])
      // increase group count so we now add characters to a new group
      groupCount  ;
      // reset character count
      charCount = 0;
    }
    charCount  ;
    return groups;
  }, [[]]);
  return characterGroups.map(group => group.join(""));
} 

const sentence = 'the quick cucumber fox jumps over the lazy dog.'
console.log(splitSentence(sentence, 15));
/* StackOverflow snippet: console should overlap rendered HTML area */
.as-console-wrapper { max-height: 100% !important; top: 0; }

CodePudding user response:

My two cents here.

let sentence = 'the quick cucumber fox jumps over the lazy dog.';
let output = [];
const limit = 15;

const check = (sentence, nchar) => {
  let tempSentence = sentence;
  if (sentence[nchar] != ' ') {
  nchar = nchar   1;
  check(sentence, nchar);
} else {
  output.push(sentence.substring(0, nchar).trim());
  tempSentence = sentence.substring(nchar, sentence.length);

  nchar = limit;
  if (tempSentence.length < nchar) {
    output.push(tempSentence.trim());
    return;
  }
  check(tempSentence, limit);
}

check(sentence, limit);
  • Related