Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

97
Visualizações
How to seperate a String into parts with less that a defined length

I have a string (this is the very very very very long string) and what I want is to split it into part with less that 25 characters and the parts must have complete words like this: this is the very very and very very long string not this is the very very ver and y very long string .

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

A reduce based approach which concatenates and collects such partials and also does limit each partial's concatenated length could be implemented similar to the next provided example code ...

function concatAndCollectPartialsOfLimitedLength(collector, str) {
  const { limit = 24, list } = collector;

  const lastIdx = list.length - 1;
  const partial = list[lastIdx];

  if (partial && (partial.length + str.length + 1 <= limit)) {
    // concatenate if partial exists and if the length of its
    // next concatenated version does not exceed the limit ...

    list[lastIdx] = [partial, str].join(' ');
  } else {
    // ... otherwise provide and collect the base
    // for the following concatenation step(s).

    list.push(str);
  }
  return collector;
}

console.log(
  'concatenated partial length is limited to 24 chars ...',
  "this is the very very very very long string and some more of it and even more"
    .trim()
    .split(/\s+/)
    .reduce(concatAndCollectPartialsOfLimitedLength, { list: [] })
    .list
);
console.log(
  'concatenated partial length is limited to 13 chars ...',
  "this is the very very very very long string and some more of it and even more"
    .trim()
    .split(/\s+/)
    .reduce(
      concatAndCollectPartialsOfLimitedLength,
      { limit: 13, list: [] }
    )
    .list
);
console.log(
  'concatenated partial length is limited to 31 chars ...',
  "this is the very very very very long string and some more of it and even more"
    .trim()
    .split(/\s+/)
    .reduce(
      concatAndCollectPartialsOfLimitedLength,
      { limit: 31, list: [] }
    )
    .list
);
.as-console-wrapper { min-height: 100%!important; top: 0; }

about 4 years ago · Juan Pablo Isaza Relatório

0

this is how I solved that:

function stringSeperator( str, n ){
    // str: your string , n: max length of each part
    let j = 0
    let sepArr = [""]
    let strArr = str.split(" ")
    for(let i=0 ; i<strArr.length ; i++){
        if(sepArr[j].length + strArr[i].length >= n){
            j++
            sepArr[j] = ""
        }
        sepArr[j] += strArr[i] + " "
    }
    return sepArr
}

which will return a array of string's parts. I also added a space to end of each part that can be omitted

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda