Return to Snippet

Revision: 10034
at December 4, 2008 09:51 by flatearthcomms


Initial Code
//   ------------------------
//   Remove space from the
//   beginning and the end
//   of a string
//   ------------------------
String.prototype.trim = function()
{
   var j, strlen, k;
   //   -----------
   //   From Begin
   //   -----------
   strlen = this.length
   j = 0;
   while (this.charAt(j) == " ")
   {
      j++
   } 
   if(j)
   {
      this = substring(this,j+1, strlen)
      if(j == strlen) return this;
   }
   //   -------------
   //   From the end
   //   -------------
   var k = this.length - 1;
   while(this.charAt(k) == " ")
   {
      k--
   }
   this = substring(this,1,k+1)
   return this;
}

//   -------------------------------
//   USAGE:
//   The trim() function will not
//   replace the original string
//   a new trim string will be
//   returned
//   -------------------------------
stringa = " This is a test text    ";
trace("original : '" + stringa + "' (len: " + stringa.length + ")");
//   create a new variables 
trimString = stringa.trim();
trace("trim string : '" + trimString + "' (len: " + trimString.length + ")");

Initial URL
www.sephiroth.it/proto_detail.php?id=15

Initial Description
Untested

Initial Title
Trim spaces from start and end

Initial Tags


Initial Language
ActionScript