FAQ Topic - How do I trim whitespace - LTRIM/RTRIM/TRIM?

F

FAQ server

-----------------------------------------------------------------------
FAQ Topic - How do I trim whitespace - LTRIM/RTRIM/TRIM?
-----------------------------------------------------------------------

Using Regular Expressions (JavaScript 1.2/JScript 4+) :

String.prototype.LTrim=new Function("return this.replace(/^\\s+/,'')")
String.prototype.RTrim=new Function("return this.replace(/\\s+$/,'')")
String.prototype.Trim=
new Function("return this.replace(/^\\s+|\\s+$/g,'')")

or for all versions (trims characters ASCII<32 not true
"whitespace"):

function LTrim(str) {
for (var k=0; k<str.length && str.charAt(k)<=" " ; k++) ;
return str.substring(k,str.length);
}
function RTrim(str) {
for (var j=str.length-1; j>=0 && str.charAt(j)<=" " ; j--) ;
return str.substring(0,j+1);
}
function Trim(str) {
return LTrim(RTrim(str));
}

http://msdn.microsoft.com/library/en-us/script56/html/js56reconIntroductionToRegularExpressions.asp

http://docs.sun.com/source/816-6408-10/regexp.htm


===
Postings such as this are automatically sent once a day. Their
goal is to answer repeated questions, and to offer the content to
the community for continuous evaluation/improvement. The complete
comp.lang.javascript FAQ is at http://www.jibbering.com/faq/.
The FAQ workers are a group of volunteers.
 

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
473,995
Messages
2,570,228
Members
46,818
Latest member
SapanaCarpetStudio

Latest Threads

Top