Posted By

jatkins on 11/07/11


Tagged

trim String text manipulation


Versions (?)

JavaScript String Trim Function


 / Published in: JavaScript
 

Released into the public domain. Returns a string with leading and trailing spaces from stringToTrim.

  1. function reverseString(strToReverse) {
  2. reversedString = '';
  3. for(var i=strToReverse.length-1;i>=0;i--)
  4. reversedString += strToReverse[i];
  5. return reversedString;
  6. }
  7.  
  8. function trimString(stringToTrim) {
  9. var sStart = 0, sEnd = 0;
  10. while(stringToTrim[sStart]==' ')
  11. sStart++;
  12. stringToTrim = reverseString(stringToTrim.substring(sStart));
  13. while(stringToTrim[sEnd]==' ')
  14. sEnd++;
  15. return reverseString(stringToTrim).substring(0, stringToTrim.length-sEnd);
  16. }

Report this snippet  

You need to login to post a comment.