Skip to content

Instantly share code, notes, and snippets.

@ricardodantas
Created April 23, 2013 18:28
Show Gist options
  • Save ricardodantas/5446108 to your computer and use it in GitHub Desktop.
Save ricardodantas/5446108 to your computer and use it in GitHub Desktop.
Javascript wrap string function.
function GetWrapedText(text, maxlength) {
var resultText = [""];
var len = text.length;
if (maxlength >= len) {
return text;
}
else {
var totalStrCount = parseInt(len / maxlength);
if (len % maxlength != 0) {
totalStrCount++
}
for (var i = 0; i < totalStrCount; i++) {
if (i == totalStrCount - 1) {
resultText.push(text);
}
else {
var strPiece = text.substring(0, maxlength - 1);
resultText.push(strPiece);
resultText.push("<br>");
text = text.substring(maxlength - 1, text.length);
}
}
}
return resultText.join("");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment