Re: FAQ Topic - How do I trim whitespace - trim/trimRight/trimLeft (2007-12-25)



In comp.lang.javascript message <47704806$0$90263$14726298@xxxxxxxxxxxxx
dk>, Tue, 25 Dec 2007 00:00:01, FAQ server <javascript@xxxxxxxxxxxxxx>
posted:

FAQ Topic - How do I trim whitespace -
trim/trimRight/trimLeft


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

String.prototype.trimLeft =
function()
{
return this.replace(/^\s+/,'');
}
...


The FAQ is intended for incomers to read. Therefore, each Topic should
explicitly answer its Subject question. That one does not. It shows
three strange statements each containing a nameless function and
something moderately inscrutable in parentheses.

Also, though the Subject refers to whitespace the answer refers only to
leading/trailing whitespace.

NOTE that whitespace can, in some contexts such as Pascal code, include
newlines; the word used alone is thus not ideal for describing what trim
trims.

.. . .

Most string manipulations are best done by using Regular Expressions.

To reduce multiple spaces/tabs within a string in S1 to single spaces :
S2 = S1.replace(/\s{2,}/, " ") // /\s{2,}/ is a RegExp literal

One can give String Objects a Method to do that by :
String.prototype.unPad =
function() { return this.replace(/\s{2,}/, " ") }

/* then continue with the trim routines as before (though the first two
are not needed, being easily derived from an understanding of the third)
and adjust the Subject of the item. */

--
(c) John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v6.05 MIME.
Web <URL:http://www.merlyn.demon.co.uk/> - FAQqish topics, acronyms & links;
Astro stuff via astron-1.htm, gravity0.htm ; quotings.htm, pascal.htm, etc.
No Encoding. Quotes before replies. Snip well. Write clearly. Don't Mail News.
.



Relevant Pages

  • Re: An Easy One for you all
    ... I have tried trim() which supposedly takes out whitespace but its not ... string, and all the whitespace after the last none-whitespace character in the ...
    (alt.php)
  • Re: regular expressions
    ... you could just use Trim() on the string which should remove any whitespace ... a simple replace on these characters with the empty string will sort it out. ... line" stuff so text is returned as a clean single line? ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: Requesting advice how to clean up C code for validating string represents integer
    ... you are assuming the string is meant to have only one data item. ... optionally with whitespace around it either/both way. ... I'll have to remember that paradigm if and when I ever ask a user ... The only place I used implicit int was in return value for main, ...
    (comp.lang.c)
  • Re: Fortran
    ... it didn't implement a working TRIM() in F77. ... CHARACTER*80 STRING ... EXTERNAL MYTRIM ... STRING = 'alongword' ...
    (comp.lang.fortran)
  • I am having a hard time with this code...
    ... delimited by whitespace, which consists of spaces, tabs, ... ; character read was ... ;string printed to report ... Subroutine to get the next character from the standard input. ...
    (alt.lang.asm)

Loading