Javascript / Text / Trim Function
Snippet details
ID: 229
Viewed: 1430
Added: 2002-05-20
Version:
Sorry no demo
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
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
