Friday, November 3

javascript Trim method

Javascript has no trim() method that is required all the time.

This script will strip starting and ending spaces for you..

   72 // JScript File
   73 //javascript left & right trim similar to stringObj.Trim() in C#
   74 function trimAll(sString)
   75 {
   76     while (sString.substring(0,1) == ' ')
   77     {
   78         sString = sString.substring(1, sString.length);
   79     }
   80     while (sString.substring(sString.length-1, sString.length) == ' ')
   81     {
   82         sString = sString.substring(0,sString.length-1);
   83     }
   84     return sString;
   85 }

Comments:
Yeah, I have often wondered about that (i.e. the fact that Javascript does not have a trim method).
 
Post a Comment

Subscribe to Post Comments [Atom]





<< Home

This page is powered by Blogger. Isn't yours?

Subscribe to Posts [Atom]