...

View Full Version : trim chars



hello20109876
05-08-2006, 03:49 AM
which java-script function can trim chars at left and right side of a string?

i.g.
original string: "\r hello \n\t ";
trimed string: "hello"

thx
:D

thesmart1
05-08-2006, 03:56 AM
Do you want the original string to return the trimmed string?

hello20109876
05-08-2006, 07:02 AM
any way to trim it.

Kravvitz
05-08-2006, 07:17 AM
Please search first next time. (click this) (http://www.google.com/search?q=JavaScript+trim+white-space)

Philip M
05-08-2006, 10:35 AM
function trim(str) {
s1 = str.replace(/^(\s)*/, '');
s2 = s1.replace(/(\s)*$/, '');
return s2;
}

Beagle
05-08-2006, 03:06 PM
assignments are expensive, it's faster this way:

function trim(str) {
return str.replace(/^\s*|\s*$/g, '');
}



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum