Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

91
Views
Cómo separar una cadena en partes con menos de una longitud definida

Tengo una cadena ( this is the very very very very long string ) y lo que quiero es dividirla en partes con menos de 25 caracteres y las partes deben tener palabras completas como esta: this is the very very very very long string no esta es la y very long string this is the very very ver y muy larga.

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Un enfoque basado en la reduce que concatene y recopile dichos parciales y también limite la longitud concatenada de cada parcial podría implementarse de manera similar al siguiente código de ejemplo proporcionado...

 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 Report

0

así es como resolví eso:

 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 }

que devolverá una matriz de partes de la cadena. También agregué un espacio al final de cada parte que se puede omitir

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!