snippet library logo
in category
Javascript / Text / Trim Function
Snippet details
ID: 229
Viewed: 1430
Added: 2002-05-20
Version:
Sorry no demo

User Rated at: 4 Stars
Rate This:
Snippets in this catagory         
Show Printable Version
I thought this would come in handy for someone wanting to trim whitespace from a string or a word.

it will trim all the whitespaces from a work like so: " hi "

it will make it so it looks like this "hi"

Browsers: all

General Details
Snippet uploaded by: snippet
Email : webmaster@snippetlibrary.com
Snippet By: punkpie_uk

<!---Head--->
<script language="javascript" type=text/javascript">
function trim(txt){
  var tmp = "";
  var item_length = txt.length;
  var item_length_minus_1 = txt.length - 1;
  for (index = 0; index < item_length; index++){
    if (txt.charAt(index) != ' '){
      tmp += txt.charAt(index);
    }else{
      if (tmp.length > 0){
        if (txt.charAt(index+1) != ' ' && index != item_length_minus_1){
          tmp += txt.charAt(index);
        }
      }
    }
  }
  return tmp;
}
</script>

<!---Body--->


// then call it like this


newtxt = trim('fk    dns  fnds gkejgij');





No Reviews to show


Please completely fill out the form below if you want to review this snippet. All reviews are subject to validation.


Subject:

Reviewed By:

Write a review:





Terms of Conditions
Powered By
Avian Hosting
© 2005 snippetlibrary.com All Rights Reserved.