no built-in trim function in javascript

M

Matt

I want to confirm there is not built-in trim() function in javascript.
If I need one, I need to implement my own needs. please advise.
Thanks!!
 
I

Ivo

I want to confirm there is not built-in trim() function in javascript.
true

If I need one, I need to implement my own needs. please advise.

<script>
var whitespace=new String(' \t\n\r\f');
function trimL(s){
if(whitespace.indexOf(s.charAt(0))!=-1){
var j=0,i=s.length;
while(j<i&&whitespace.indexOf(s.charAt(j))!=-1)
j++;
s=s.substring(j,i);
}
return s;
}
function trimR(s){
if(whitespace.indexOf(s.charAt(s.length-1))!=-1){
var i=s.length-1;
while(i>=0&&whitespace.indexOf(s.charAt(i))!=-1)
i--;
s=s.substring(0,i+1);
}
return s;
}
function trim(s){
return trimR(trimL(s));
}
Welcome.
Ivo
 
D

Dr John Stockton

JRS: In article <[email protected]>, dated Wed,
28 Jul 2004 05:10:30, seen in Ivo
<script>
var whitespace=new String(' \t\n\r\f');
function trimL(s){
if(whitespace.indexOf(s.charAt(0))!=-1){
var j=0,i=s.length;
while(j<i&&whitespace.indexOf(s.charAt(j))!=-1)
j++;
s=s.substring(j,i);
}
return s;
}
function trimR(s){
if(whitespace.indexOf(s.charAt(s.length-1))!=-1){
var i=s.length-1;
while(i>=0&&whitespace.indexOf(s.charAt(i))!=-1)
i--;
s=s.substring(0,i+1);
}
return s;
}
function trim(s){
return trimR(trimL(s));
}
</script>

Why do you present that in preference to what is in the FAQ?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,678
Members
48,796
Latest member
Greg L.

Latest Threads

Top