Skip to content

Instantly share code, notes, and snippets.

@ripper2hl
Created October 14, 2015 18:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ripper2hl/fa6e06df439d426d9c31 to your computer and use it in GitHub Desktop.
Save ripper2hl/fa6e06df439d426d9c31 to your computer and use it in GitHub Desktop.
Divide una cadena de texto cada N caracteres Ejemplo: texto 'cadena', numeroCaracteres '2' resultado ['ca','de','na']
/**
* Divide una cadena de texto cada N
* caracteres
* Ejemplo:
* texto 'cadena', numeroCaracteres '2'
* resultado ['ca','de','na']
* @param texto texto a dividir
* @param numeroCaracteres numero de caracteres para su division
* @author Jesus Perales.
*/
function dividirCadenaPorNumeroCaracteres(texto, numeroCaracteres){
var listaTexto = []; //Declaracion del arreglo
var inicio = 0; //Inicio del corte de cadena
var fin = numeroCaracteres; // Fin del primer corte
var limiteCiclo = Math.ceil( texto.length / numeroCaracteres); //Numero de veces a cortar la cadena
console.log(limiteCiclo);
for(var i = 0; i < limiteCiclo; i++){
listaTexto.push( texto.substring(inicio, fin ) );//Cortamos la cadena por el inicio y por el final
inicio = fin;//El inicio se convierte en final
fin += numeroCaracteres;//El final se suma el numero de caracteres a cortar
}
return listaTexto;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment