Trim spaces from start and end


/ Published in: ActionScript
Save to your folder(s)

Untested


Copy this code and paste it in your HTML
  1. // ------------------------
  2. // Remove space from the
  3. // beginning and the end
  4. // of a string
  5. // ------------------------
  6. String.prototype.trim = function()
  7. {
  8. var j, strlen, k;
  9. // -----------
  10. // From Begin
  11. // -----------
  12. strlen = this.length
  13. j = 0;
  14. while (this.charAt(j) == " ")
  15. {
  16. j++
  17. }
  18. if(j)
  19. {
  20. this = substring(this,j+1, strlen)
  21. if(j == strlen) return this;
  22. }
  23. // -------------
  24. // From the end
  25. // -------------
  26. var k = this.length - 1;
  27. while(this.charAt(k) == " ")
  28. {
  29. k--
  30. }
  31. this = substring(this,1,k+1)
  32. return this;
  33. }
  34.  
  35. // -------------------------------
  36. // USAGE:
  37. // The trim() function will not
  38. // replace the original string
  39. // a new trim string will be
  40. // returned
  41. // -------------------------------
  42. stringa = " This is a test text ";
  43. trace("original : '" + stringa + "' (len: " + stringa.length + ")");
  44. // create a new variables
  45. trimString = stringa.trim();
  46. trace("trim string : '" + trimString + "' (len: " + trimString.length + ")");

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

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.