Thursday, October 7, 2010

How to trim string in javascript?

<script language="JavaScript" type="text/javascript">
<!--
        String.prototype.trim = function() {
            return this.replace(/^\s*/, "").replace(/\s*$/, "");
        }

        var s = new String(" Hello ");
        // use it like this
        s = s.trim();
        alert("!" + s + "!");

// end hiding contents -->
</script>

No comments:

Post a Comment