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



In comp.lang.javascript message <1193957400.023804.169070@xxxxxxxxxxxxxx
oglegroups.com>, Thu, 1 Nov 2007 22:50:00, Peter Michaux
<petermichaux@xxxxxxxxx> posted:

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);
}

It is often considered good practice for the terminal condition of a FOR
loop to be constant during that loop, to reduce confusion. Therefore
(undertested) :-

function lTrim(str) { var k = 0
while( k<str.length && str.charAt(k)<=" ") k++
return str.substring(k, str.length);
}

function rTrim(str) { var k = str.length-1
while (k>=0 && str.charAt(k)<=" ") k--
return str.substring(0, k+1);
}

Functions, except maybe when one-liners, should be separated by vertical
whitespace, for legibility. And k is better than j.

The character l should not be used where it might be, even momentarily,
thought to be a 1. Therefore, the names might be changed to trimL and
for symmetry trimR, matching trim in case. To facilitate searching for
the identifier, I suggest trim be renamed to trimX (X for eXtremes).

And "ASCII<32" is wrong, since the characters are UniCode.

--
(c) John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v6.05 MIME.
Web <URL:http://www.merlyn.demon.co.uk/> - FAQish topics, acronyms, & links.
For news:borland.*, use their server newsgroups.borland.com ; but first read
Guidelines <URL:http://www.borland.com/newsgroups/guide.html> ff. with care.
.



Relevant Pages

  • Re: add a character to a line...
    ... How do you add a word or a character to the beginning of a line in a ... I want to add a comment '#' where the string 'var' appears in a ... It's easy to put this in a for loop to process several files. ...
    (comp.unix.programmer)
  • Re: looking for implementation of strtok
    ... contains a ',' character. ... we enter the ugly loop that simply implements strspn: ... loop sets spanp to delim, then loops while seting sc (the "span ... If there are no more tokens, we set *lastp to NULL (this is, I ...
    (comp.lang.c)
  • Re: Event Based or Get-Key based
    ... say, much like you'd program a GUI - the main loop just collects input, and that input is then passed to call backs. ... the queue when character death is detected. ... Executing an ENDGAME event removes all pending events ... it queues up a select-action ...
    (rec.games.roguelike.development)
  • Re: Armenian, Sumerian, Burushaski, and Turkic languages
    ... - sort of a blank character, ... maybe it really is a Norwegian thing. ... Det var en liten gutt som gikk og græt og var så lei, ... med blanke ark og fargestifter tel. ...
    (sci.lang)
  • Re: Which assembler can handle the BIG stuff ?
    ... |> jnz loop;or JNS/JNGE if endmark included in size ... | Yeah, but this is done once, not once per character, so it isn't going ... |> Ok, I'd use KESYS Fn50 to create a formatted, sorted table ... I don't sort ...
    (alt.lang.asm)