1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Trim function JavaScript/JScript

Discussion in 'JavaScript and AJAX' started by pradeep, Dec 1, 2006.

  1. pradeep

    pradeep Team Leader

    Joined:
    Apr 4, 2005
    Messages:
    1,646
    Likes Received:
    86
    Trophy Points:
    0
    Occupation:
    Programmer
    Location:
    Kolkata, India
    Home Page:
    I needed to trim a string and also replace more than one spaces with a single space. The solution was simple with String.replace in JavaScript. Here's the code:

    Code:
    var m = " My name  is Pradeep ";
    m = m.replace(/^[\s]+/,'').replace(/[\s]+$/,'').replace(/[\s]{2,}/,' ');
    Making a function out of it.
    Code:
    function trim(str)
    {
        if(!str || typeof str != 'string')
            return null;
    
        return str.replace(/^[\s]+/,'').replace(/[\s]+$/,'').replace(/[\s]{2,}/,' ');
    }
     
  2. Parimal

    Parimal New Member

    Joined:
    May 4, 2007
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    bekar
    Location:
    pagalkhana
  3. urstop

    urstop New Member

    Joined:
    Oct 17, 2007
    Messages:
    84
    Likes Received:
    0
    Trophy Points:
    0
    Good use of regular expressions, we wish there was a simpler method like vbscript where in we can just use the function trim.
     
  4. prasanna_gg

    prasanna_gg New Member

    Joined:
    Mar 10, 2009
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    Location:
    Melbourne, Australia
    Hi Pradeep,

    Nice solution. Although it could be extended nicely so it works natively in Javascript. JS provides a way to create prototype functions easily.

    Here is the code:

    Code:
    String.prototype.trim = function() {  return this.replace(/^\s+|\s+$/g, '');  }  
    This method can be called like any in-built javascript function - string.trim() instead of trim(string)!
     
  5. gkumar

    gkumar New Member

    Joined:
    Jun 16, 2009
    Messages:
    58
    Likes Received:
    5
    Trophy Points:
    0
    Use the code below to make trim a method of all Strings. These are useful to place in a global Javascript file included by all your pages.

    Code:
    String.prototype.trim = function() {
    	return this.replace(/^\s+|\s+$/g,"");
    }
    String.prototype.ltrim = function() {
    	return this.replace(/^\s+/,"");
    }
    String.prototype.rtrim = function() {
    	return this.replace(/\s+$/,"");
    }
    
    // example of using trim, ltrim, and rtrim
    var myString = " hello my name is ";
    alert("*"+myString.trim()+"*");
    alert("*"+myString.ltrim()+"*");
    alert("*"+myString.rtrim()+"*");
    
    Javascript Trim Stand-Alone Functions:-If you prefer not to modify the string prototype, then you can use the stand-alone functions below.

    Code:
    function trim(stringToTrim) {
    	return stringToTrim.replace(/^\s+|\s+$/g,"");
    }
    function ltrim(stringToTrim) {
    	return stringToTrim.replace(/^\s+/,"");
    }
    function rtrim(stringToTrim) {
    	return stringToTrim.replace(/\s+$/,"");
    }
    
    // example of using trim, ltrim, and rtrim
    var myString = " hello my name is ";
    alert("*"+trim(myString)+"*");
    alert("*"+ltrim(myString)+"*");
    alert("*"+rtrim(myString)+"*");
     
  6. pradeep

    pradeep Team Leader

    Joined:
    Apr 4, 2005
    Messages:
    1,646
    Likes Received:
    86
    Trophy Points:
    0
    Occupation:
    Programmer
    Location:
    Kolkata, India
    Home Page:
    Nice one gkumar
     
  7. dubeyparam

    dubeyparam New Member

    Joined:
    Jul 2, 2010
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
  8. Full Zip Hoody

    Full Zip Hoody New Member

    Joined:
    Sep 29, 2010
    Messages:
    20
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Programer
    Location:
    US of A
    it works fine for me both ways. the most important thing is that there is a way of doing this without pretty much effort at all.
     

Share This Page