Created
July 4, 2017 08:18
-
-
Save dehghani-mehdi/4868c5f70e5604c7362b0c9c58f304c2 to your computer and use it in GitHub Desktop.
C# like trimEnd method in JavaScript
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
String.prototype.trimEnd = function(c) { | |
if (this.length == 0) return this; | |
c = c ? c : ' '; | |
var i = this.length - 1; | |
for (; i >= 0 && this.charAt(i) == c; i--); | |
return this.substring(0, i + 1); | |
} | |
// Example | |
"hello world! ".trimEnd(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment