Last active
August 29, 2015 14:00
-
-
Save xxl007/11125176 to your computer and use it in GitHub Desktop.
Trimming Whitespace from the Ends of a String
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
// Using the trim method (trimLeft and trimRight methods also exist) | |
var txtBox = document.getElementById("test"); | |
var line = txtBox.value.split("\n"); | |
var resultString = ""; | |
for (var i=0; i < lines.length; i++) { | |
var strng = lines[i].trim(); | |
resultString += strng + "-"; | |
} | |
alert(resultString); | |
// Workaround in case trim method not supported in browser (pre ECMAScript 5) | |
// adding customised trim method to the String object | |
// Subsequently call the trim method on any String | |
if (typeof String.trim == "undefined") { | |
String.prototype.trim = function () { | |
return this.replace(/(^\s*)|(\s*$)/g, ""); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment