Last active
June 20, 2016 15:59
-
-
Save adityatejas/7857c0866f67783e71a1c9d60d3beed8 to your computer and use it in GitHub Desktop.
Truncate a string
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 truncateString(str, num) { | |
// Clear out that junk in your trunk | |
var truncatedStr = ''; | |
if (str.length > num) { | |
truncatedStr = str.slice(0, num-3) + "..."; | |
return truncatedStr; | |
} | |
else if (str.length <= 3) | |
{ | |
truncatedStr = str.slice(0, num) + "..."; | |
return truncatedStr; | |
} | |
else return str; | |
} | |
truncateString("Adi", 1); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment