Created
October 14, 2015 18:55
-
-
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']
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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