Created
April 23, 2013 18:28
-
-
Save ricardodantas/5446108 to your computer and use it in GitHub Desktop.
Javascript wrap string function.
This file contains 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
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