Wednesday, November 02, 2005
Here is a free JavaScript trim() function for your use. "Trim" is a common function in other languages' syntax that removes leading and trailing spaces in a string. It does not exist in JavaScript as a built-in String function.

Notes:
  • There are 3 versions of trim() functions below, all functionally equivalent:
    • The first trim() function listed below operates as a single function.
    • The second trim() function listed below is an aggregation of 3 other functions that are also independently useful: leftTrim() - removes leading whitespace, rightTrim() - removes trailing whitespace, and reverseString() which returns a given string in reverse order backwards (sdrawkcab)
    • The last trim() function listed below adds the trim() function directly to the String object.  This allows you to call trim by using the syntax sFirstName.trim() instead of trim(sFirstName).  It utilizes the prototype functionality of JavaScript to extend objects, which makes the code backwards-compatible to JavaScript 1.1.
  • These versions of trim() do not use the String.replace function in order to be backwards-compatible to JavaScript 1.0.  Using regular expressions (via String.replace) would produce a smaller function but make it only backwards-compatible to JavaScript 1.2. 
  • rightTrim() requires reverseString(), but could be refactored into a less "modularized" function.
    //compact, stand-alone version of trim()
    function trim(sVal)
    {
    	sTrimmed = "";
    				
    	for (i = 0; i < sVal.length; i++)
    	{
    		if (sVal.charAt(i) != " "
    			&& sVal.charAt(i) != "\f"
    			&& sVal.charAt(i) != "\n"
    			&& sVal.charAt(i) != "\r"
    			&& sVal.charAt(i) != "\t")
    		{
    			sTrimmed = sTrimmed + sVal.charAt(i);
    		}
    	}
    
    	return sTrimmed;
    }
    
    //modularized version of trim()
    function trim(sVal)
    {
    	return rightTrim(leftTrim(sVal));
    }
    
    function leftTrim(sVal)
    {
    	sTrimmed = "";
    	bFoundNonBlank = false;
    				
    	for (i = 0; i < sVal.length; i++)
    	{
    		if (bFoundNonBlank
    			|| (sVal.charAt(i) != " "
    			&& sVal.charAt(i) != "\f"
    			&& sVal.charAt(i) != "\n"
    			&& sVal.charAt(i) != "\r"
    			&& sVal.charAt(i) != "\t"))
    		{
    			sTrimmed = sTrimmed + sVal.charAt(i);
    			bFoundNonBlank = true;
    		}
    	}
    
    	return sTrimmed;
    }
    
    function rightTrim(sVal)
    {
    	sTrimmed = "";
    	bFoundNonBlank = false;
    				
    	for (i = sVal.length-1; i >= 0; i--)
    	{
    		if (bFoundNonBlank
    			|| (sVal.charAt(i) != " "
    			&& sVal.charAt(i) != "\f"
    			&& sVal.charAt(i) != "\n"
    			&& sVal.charAt(i) != "\r"
    			&& sVal.charAt(i) != "\t"))
    		{
    			sTrimmed = sTrimmed + sVal.charAt(i);
    			bFoundNonBlank = true;
    		}
    	}
    
    	return reverseString(sTrimmed);
    }
    
    function reverseString(sVal)
    {
    	sReverse = "";
    
    	for (i = sVal.length; i >= 0; i--)
    	{
    		sReverse += sVal.charAt(i);
    	}
    
    	return sReverse;
    }
    
    //stand-alone version added to String object
    String.prototype.trim = function()
    {
    
    	sVal = this;
    	sTrimmed = "";
    				
    	for (i = 0; i < sVal.length; i++)
    	{
    		if (sVal.charAt(i) != " "
    			&& sVal.charAt(i) != "\f"
    			&& sVal.charAt(i) != "\n"
    			&& sVal.charAt(i) != "\r"
    			&& sVal.charAt(i) != "\t")
    		{
    			sTrimmed = sTrimmed + sVal.charAt(i);
    		}
    	}
    
    	return sTrimmed;
    }
    
    
11/2/2005 5:17:57 PM (Central Daylight Time, UTC-05:00)  #    Disclaimer  |  Comments [0]  | 
Related Posts:
.NET styled framework for Javascript
GetToken function in JavaScript to return a value from a delimited string