Trim Leading And Trailing Spaces

Trim the whitespaces at the beginning and end of a string using while loops instead of regular expressions.
  1. function TrimString(strToTrim)
  2. {
  3.  while('' + strToTrim.charAt(0) == ' ')
  4.  {
  5.         strToTrim = strToTrim.substring(1, strToTrim.length);
  6.  }
  7.  while('' + strToTrim.charAt(strToTrim.length - 1) == ' ')
  8.  {
  9.         strToTrim = strToTrim.substring(0, strToTrim.length - 1);
  10.  }
  11.  return strToTrim;
  12. }

Leave a Reply

Your email address will not be published. Required fields are marked *

Back To Top