473,385 Members | 1,752 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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

-----------------------------------------------------------------------
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/de...63906a7353.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://jibbering.com/faq/index.html.
The FAQ workers are a group of volunteers.

Feb 22 '07 #1
27 6773
FAQ server wrote:
-----------------------------------------------------------------------
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,'')")
What would also be useful is another prototype that reduces multiple
contiguous spaces to a single space.

I don't have it at hand but someone here kindly wrote one for me.

Andrew Poulos
Feb 22 '07 #2
Andrew Poulos wrote on 22 feb 2007 in comp.lang.javascript:
FAQ server wrote:
>-----------------------------------------------------------------------
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,'')")

What would also be useful is another prototype that reduces multiple
contiguous spaces to a single space.
String.prototype.whiteSpaces2singleSpace =
new Function("return this.replace(/\\s\\s+/g,' ')")

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Feb 22 '07 #3
Evertjan. <ex**************@interxnl.netwrote:
String.prototype.whiteSpaces2singleSpace =
new Function("return this.replace(/\\s\\s+/g,' ')")

writing :

....(/\s\s+/g,' ')...

isn't sufficient no need to escape \ ???

ami i wrong ?
--
Une Bévue
Feb 22 '07 #4
Une Bévue wrote on 22 feb 2007 in comp.lang.javascript:
Evertjan. <ex**************@interxnl.netwrote:
>String.prototype.whiteSpaces2singleSpace =
new Function("return this.replace(/\\s\\s+/g,' ')")


writing :

...(/\s\s+/g,' ')...

isn't sufficient no need to escape \ ???

ami i wrong ?
Did you try?

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Feb 22 '07 #5
Evertjan. <ex**************@interxnl.netwrote:
Did you try?
YES here it is :

<http://www.yvon-thoraval.com/JS/test_regexp.html>

and i'm TRUE ;-)
--
Une Bévue
Feb 22 '07 #6
Une Bévue wrote on 22 feb 2007 in comp.lang.javascript:
Evertjan. <ex**************@interxnl.netwrote:
>Did you try?

YES here it is :

<http://www.yvon-thoraval.com/JS/test_regexp.html>
Nonsense, you did not try,
but did quite someting else!

You did this:

var s = p.replace(/\s\s+/g, ' ');

While my line was:

String.prototype.whiteSpaces2singleSpace =
new Function("return this.replace(/\\s\\s+/g,' ')")

Please try that with /\\s\\s+/g replaced by /\s\s+/g

.... and you will see what goes wrong.

Do you know what goes wrong?
and i'm TRUE ;-)
Are you Boolean?

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Feb 22 '07 #7
Evertjan. <ex**************@interxnl.netwrote:
Please try that with /\\s\\s+/g replaced by /\s\s+/g
i have added the /\\s\\s+/g for t right now
Do you know what goes wrong?
Not at all because i thought the purpose of the Regexp was to replace

multiple white spaces by single one, as the name :

String.prototype.whiteSpace2singleSpace implies ???
--
Une Bévue
Feb 22 '07 #8
In article <1h**********************************@google.com.i nvalid>,
un************@google.com.invalid (Une Bévue) wrote:
Evertjan. <ex**************@interxnl.netwrote:
Please try that with /\\s\\s+/g replaced by /\s\s+/g

i have added the /\\s\\s+/g for t right now
Do you know what goes wrong?

Not at all because i thought the purpose of the Regexp was to replace

multiple white spaces by single one, as the name :

String.prototype.whiteSpace2singleSpace implies ???
Safari doesn't like your toolkit.js as there is an extra , on line 348
or so. FF didn't seem to mind.

-- tim
Feb 22 '07 #9
Une Bévue wrote on 22 feb 2007 in comp.lang.javascript:
Evertjan. <ex**************@interxnl.netwrote:
>Please try that with /\\s\\s+/g replaced by /\s\s+/g

i have added the /\\s\\s+/g for t right now
>Do you know what goes wrong?

Not at all because i thought the purpose of the Regexp was to replace

multiple white spaces by single one, as the name :

String.prototype.whiteSpace2singleSpace implies ???
You are on the wrong track.
>String.prototype.whiteSpaces2singleSpace =
new Function("return this.replace(/\\s\\s+/g,' ')");
new Function() here has a string as parameter,
and in javascript strings backslashes are used as escape characters.
If you need the backslashes, you will have to escape them themselves.
This is ONLY required in a string,
not in a regex declaration without a string.

Try this:

====================================
<script type='text/javascript'>

alert("return this.replace(/\s\s+/g,' ')");

alert("return this.replace(/\\s\\s+/g,' ')");

</script>
=====================================


--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Feb 22 '07 #10
Evertjan. <ex**************@interxnl.netwrote:
new Function() here has a string as parameter,
and in javascript strings backslashes are used as escape characters.
If you need the backslashes, you will have to escape them themselves.
This is ONLY required in a string,
not in a regex declaration without a string.
OK, i see what u mean )))

BUT why did them use :

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

i don'undetstand why u (them) are passing a constant as an argument ???
--
Une Bévue
Feb 22 '07 #11
Tim Streater <ti**********@dante.org.ukwrote:
>
Safari doesn't like your toolkit.js as there is an extra , on line 348
or so. FF didn't seem to mind.
Fine, thanks, repaired ))

i let the last "," in order not to forget it when cut'n paste )))
--
Une Bévue
Feb 22 '07 #12
Une Bévue wrote on 22 feb 2007 in comp.lang.javascript:
Evertjan. <ex**************@interxnl.netwrote:
>new Function() here has a string as parameter,
and in javascript strings backslashes are used as escape characters.
If you need the backslashes, you will have to escape them themselves.
This is ONLY required in a string,
not in a regex declaration without a string.

OK, i see what u mean )))

BUT why did them use :

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

i don'undetstand why u (them) are passing a constant as an argument ???
No, it is called a string litteral, javascript has no constants.

I don't know why, both work.

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Feb 22 '07 #13
Evertjan. <ex**************@interxnl.netwrote:
No, it is called a string litteral, javascript has no constants.
Ok. However a string litteral which doesn't vary ))
I don't know why, both work.
i never used that kind of syntax for a function :

new Function("what u want to be done");

i did a little experiment like that :

String.prototype.hello= new Function("alert('Hello World!')");
"".hello();

and it works fine !
--
Une Bévue
Feb 22 '07 #14
On Feb 21, 8:15 pm, Andrew Poulos <ap_p...@hotmail.comwrote:
FAQ server wrote:
-----------------------------------------------------------------------
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,'')")

What would also be useful is another prototype that reduces multiple
contiguous spaces to a single space.
So, am I the only one who finds this syntax horrible to look at?
Isn't this easier?

// converted to camelCase to match every other member function in
Javascript.
String.prototype.lTrim=function () { return this.replace(/^\s
+/,''); };
String.prototype.rTrim=function () { return this.replace(/\s+
$/,''); };
String.prototype.trim= function () { return this.replace(/^\s+|\s+$/
g,''); };

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

What's the advantage of using
new Function( 'blah blah blah' )
instead of
function () { blah blah blah }
besides increasing the risk of errors due to unescaped characters?

In my opinion, as a general rule, eval is evil. The same goes for all
the other cases where a string literal is executed as code.
They open up the door for simple errors that are tricky to catch. The
browsers that required these approaches are all but extinct, and new
systems tend to implement the native function object approach. If
there is any other way to get the job done, it's probably best to
avoid executing a string as code.

--
Isaac Z. Schlueter
http://isaacschlueter.com

Feb 23 '07 #15
Isaac Schlueter wrote on 23 feb 2007 in comp.lang.javascript:
On Feb 21, 8:15 pm, Andrew Poulos <ap_p...@hotmail.comwrote:
>>
What would also be useful is another prototype that reduces multiple
contiguous spaces to a single space.

So, am I the only one who finds this syntax horrible to look at?
Isn't this easier?

// converted to camelCase to match every other member function in
Javascript.
String.prototype.lTrim=function () { return this.replace(/^\s
+/,''); };
String.prototype.rTrim=function () { return this.replace(/\s+
$/,''); };
String.prototype.trim= function () { return this.replace(/^\s+|\s+$/
g,''); };

String.prototype.singleSpace= function () { return
this.replace(/\s\s+/ g,' '); };
I think you are right on both accounts,
the lowercase naming and the eventual evil eval-ness.
>
What's the advantage of using
new Function( 'blah blah blah' )
instead of
function () { blah blah blah }
besides increasing the risk of errors due to unescaped characters?
The only advantage could be dynamic defining by manipulation of the
defining string, but even here, you can easily do without:

function makeTrimProto(name,rgx){
String.prototype[name] = function ()
{ return this.replace(rgx,' '); };
};

makeTrimProto('lTrim', /^\s+/);
makeTrimProto('rTrim', /\s+$/);
makeTrimProto('trim', /^\s+|\s+$/g);
makeTrimProto('singleSpace', /\s\s+/g);
In my opinion, as a general rule, eval is evil.
[..]

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Feb 23 '07 #16
On Feb 23, 8:40 am, "Evertjan." <exjxw.hannivo...@interxnl.netwrote:
Isaac Schlueter wrote on 23 feb 2007 in comp.lang.javascript:
<snip>
>What's the advantage of using
new Function( 'blah blah blah' )
instead of
function () { blah blah blah }
besides increasing the risk of errors due to unescaped characters?

The only advantage could be dynamic defining by manipulation of the
defining string, but even here, you can easily do without:
<snip>

That is not the only reason (and it easily could be argued that it is
not a good idea to be dynamically manipulating script code on the
client, it certainly introduces an additional layer of issues that
probably should be avoided). The other good reason for using the
Function constructor is to create function objects that have a minimum
scope chain from a context that would impose a more involved scope
chain on any inner functions created within it.

<FAQENTRY>Randy, these trim function examples probably are candidates
for moving away from function constructor use to the use of function
expressions. Because they are - String.prototype - extensions we can
be pretty sure they will be evaluated in the global execution context
and so the scope chains of the functions will not differ depending on
the creation method, and by now we must be past the point where
continuing to accommodate browsers that did not understand function
expressions (pre-IE4, Netscape 4) is pointless.</FAQENTRY>

Richard.
Feb 23 '07 #17
In comp.lang.javascript message <11**********************@h3g2000cwc.goo
glegroups.com>, Fri, 23 Feb 2007 02:51:36, Richard Cornford
<Ri*****@litotes.demon.co.ukposted:
>by now we must be past the point where
continuing to accommodate browsers that did not understand
<FAQENTRY>

IMHO, the FAQ should contain a statement along the general lines of

"In general, the code in this FAQ is for MSIE 4, Netscape 5, Opera 6,
Firefox 7, and equivalent or later browsers. Much of it will work on
older systems. Where code will not work on those above, or is present
specifically to support earlier browsers, that will be noted in or by
the code."

If a draft subsection for that is posted here, with a list of the places
where such markings are known to be needed, then discussion should
quickly enough yield a generally-acceptable list of browsers and places
to be marked.

Consider also, though, javascript in non-browsers.

--
(c) John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Delphi 3? Turnpike 6.05
<URL:http://www.merlyn.demon.co.uk/TP/BP/Delphi/&c., FAQqy topics & links;
<URL:http://www.bancoems.com/CompLangPascalDelphiMisc-MiniFAQ.htmclpdmFAQ;
<URL:http://www.borland.com/newsgroups/guide.htmlnews:borland.* Guidelines
Feb 23 '07 #18
Richard Cornford said the following on 2/23/2007 5:51 AM:
<FAQENTRY>Randy, these trim function examples probably are candidates
for moving away from function constructor use to the use of function
expressions.
The code that I now have locally is this:

String.prototype.LTrim=function(){return this.replace(/^\s+/,'')}
String.prototype.RTrim=function(){return this.replace(/\s+$/,'')}
String.prototype.Trim=function(){return this.replace(/^\s+|\s+$/g,'')}

If there are not problems/errors with it I will get it uploaded.

The Number.toFixed entry and the DynWrite entries also use new Function.

I will work on those two tonight and tomorrow (to do some testing) and
post the proposed changes to the code for review.

--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq/index.html
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Feb 24 '07 #19
In comp.lang.javascript message <96********************@telcove.net>,
Sat, 24 Feb 2007 14:50:51, Randy Webb <Hi************@aol.composted:
>The code that I now have locally is this:

String.prototype.LTrim=function(){return this.replace(/^\s+/,'')}
String.prototype.RTrim=function(){return this.replace(/\s+$/,'')}
String.prototype.Trim=function(){return this.replace(/^\s+|\s+$/g,'')}
Others have already asked you to make better use of whitespace in FAQ
code.
String.prototype.LTrim =
function() { return this.replace(/^\s+/, "") }
String.prototype.RTrim =
function() { return this.replace(/\s+$/, "") }
String.prototype.Trim =
function() { return this.replace(/^\s+|\s+$/g, "") }
--
(c) John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Delphi 3? Turnpike 6.05
<URL:http://www.merlyn.demon.co.uk/TP/BP/Delphi/&c., FAQqy topics & links;
<URL:http://www.bancoems.com/CompLangPascalDelphiMisc-MiniFAQ.htmclpdmFAQ;
<URL:http://www.borland.com/newsgroups/guide.htmlnews:borland.* Guidelines
Feb 25 '07 #20
Dr J R Stockton said the following on 2/25/2007 9:33 AM:
In comp.lang.javascript message <96********************@telcove.net>,
Sat, 24 Feb 2007 14:50:51, Randy Webb <Hi************@aol.composted:
>The code that I now have locally is this:

String.prototype.LTrim=function(){return this.replace(/^\s+/,'')}
String.prototype.RTrim=function(){return this.replace(/\s+$/,'')}
String.prototype.Trim=function(){return this.replace(/^\s+|\s+$/g,'')}

Others have already asked you to make better use of whitespace in FAQ
code.
They have? When and where? Besides, the only thing I changed -
whitespace wise - was the Trim line and that was more for Usenet posting
than anything. Either way, it is staying the way it is.

--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq/index.html
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Feb 25 '07 #21
Randy Webb wrote on 25 feb 2007 in comp.lang.javascript:
Dr J R Stockton said the following on 2/25/2007 9:33 AM:
>In comp.lang.javascript message <96********************@telcove.net>,
Sat, 24 Feb 2007 14:50:51, Randy Webb <Hi************@aol.composted:
>>The code that I now have locally is this:

String.prototype.LTrim=function(){return this.replace(/^\s+/,'')}
String.prototype.RTrim=function(){return this.replace(/\s+$/,'')}
String.prototype.Trim=function(){return this.replace(/^\s+|\s+$/g,'')}

Others have already asked you to make better use of whitespace in FAQ
code.

They have? When and where? Besides, the only thing I changed -
whitespace wise - was the Trim line and that was more for Usenet posting
than anything. Either way, it is staying the way it is.
I did ask for more white space.

It makes demo code more easily readable, IMHO.

btw, I would prefer the use of ;s too:

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

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Feb 25 '07 #22
On Feb 25, 1:27 pm, "Evertjan." <exjxw.hannivo...@interxnl.netwrote:
Randy Webb wrote on 25 feb 2007 in comp.lang.javascript:
Dr J R Stockton said the following on 2/25/2007 9:33 AM:
In comp.lang.javascript message <96WdnamEVf6TC33Y4p2...@telcove.net>,
Sat, 24 Feb 2007 14:50:51, Randy Webb <HikksNotAtH...@aol.composted:
The code that I now have locally is this:
>String.prototype.LTrim=function(){return this.replace(/^\s+/,'')}
String.prototype.RTrim=function(){return this.replace(/\s+$/,'')}
String.prototype.Trim=function(){return this.replace(/^\s+|\s+$/g,'')}
Others have already asked you to make better use of whitespace in FAQ
code.
They have? When and where? Besides, the only thing I changed -
whitespace wise - was the Trim line and that was more for Usenet posting
than anything. Either way, it is staying the way it is.

I did ask for more white space.

It makes demo code more easily readable, IMHO.

btw, I would prefer the use of ;s too:

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

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
By the way, is there a naming convention for method names vs. object
names? Seems perhaps more consistent with builtin methods to use
camelCase instead of PascalCase for methods, thus String.lTrim,
String.rTrim, and String.trim (compare String.charAt,
String.toLowerCase, string.indexOf) should be preferred to
String.LTrim, String.RTrim, and String.Trim, no?

-David

Feb 25 '07 #23
David Golightly wrote on 25 feb 2007 in comp.lang.javascript:
On Feb 25, 1:27 pm, "Evertjan." <exjxw.hannivo...@interxnl.netwrote:
>Randy Webb wrote on 25 feb 2007 in comp.lang.javascript:
Dr J R Stockton said the following on 2/25/2007 9:33 AM:
In comp.lang.javascript message
<96WdnamEVf6TC33Y4p2...@telcove.net>, Sat, 24 Feb 2007 14:50:51,
Randy Webb <HikksNotAtH...@aol.composted:
The code that I now have locally is this:
>>String.prototype.LTrim=function(){return this.replace(/^\s+/,'')}
String.prototype.RTrim=function(){return this.replace(/\s+$/,'')}
String.prototype.Trim=function(){return
this.replace(/^\s+|\s+$/g,'')}
>Others have already asked you to make better use of whitespace in
FAQ code.
They have? When and where? Besides, the only thing I changed -
whitespace wise - was the Trim line and that was more for Usenet
posting than anything. Either way, it is staying the way it is.

I did ask for more white space.

It makes demo code more easily readable, IMHO.

btw, I would prefer the use of ;s too:

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

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)

By the way, is there a naming convention for method names vs. object
names? Seems perhaps more consistent with builtin methods to use
camelCase instead of PascalCase for methods, thus String.lTrim,
String.rTrim, and String.trim (compare String.charAt,
String.toLowerCase, string.indexOf) should be preferred to
String.LTrim, String.RTrim, and String.Trim, no?
This was already discussed in this thread,
however without consensus result.

I agree with your point.

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Feb 25 '07 #24
Evertjan. said the following on 2/25/2007 4:27 PM:
Randy Webb wrote on 25 feb 2007 in comp.lang.javascript:
>Dr J R Stockton said the following on 2/25/2007 9:33 AM:
>>In comp.lang.javascript message <96********************@telcove.net>,
Sat, 24 Feb 2007 14:50:51, Randy Webb <Hi************@aol.composted:
The code that I now have locally is this:

String.prototype.LTrim=function(){return this.replace(/^\s+/,'')}
String.prototype.RTrim=function(){return this.replace(/\s+$/,'')}
String.prototype.Trim=function(){return this.replace(/^\s+|\s+$/g,'')}
Others have already asked you to make better use of whitespace in FAQ
code.
They have? When and where? Besides, the only thing I changed -
whitespace wise - was the Trim line and that was more for Usenet posting
than anything. Either way, it is staying the way it is.

I did ask for more white space.

It makes demo code more easily readable, IMHO.
I don't agree with the "more easily readable" but you want whitespace,
you get it.
btw, I would prefer the use of ;s too:
Yeah, makes it more "readable"
String.prototype.LTrim =
function() {
return this.replace(/^\s+/, '');
};
Changed. The names are also changed:

String.prototype.lTrim=
function()
{
return this.replace(/^\s+/,'');
}
String.prototype.lTrim=
function()
{
return this.replace(/\s+$/,'');
}
String.prototype.trim=
function()
{
return this.replace(/^\s+|\s+$/g,'');
}

--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq/index.html
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Feb 26 '07 #25
Randy Webb wrote on 26 feb 2007 in comp.lang.javascript:
Evertjan. said the following on 2/25/2007 4:27 PM:
>Randy Webb wrote on 25 feb 2007 in comp.lang.javascript:
[...]
>>
It makes demo code more easily readable, IMHO.

I don't agree with the "more easily readable"
I ment for a newbee, as the FAQ is most useful to.
Later on it seems handy to compact these library-like functions
as not to clutter the "real" programming.
but you want whitespace, you get it.
Thanks, got it. ;-)
>btw, I would prefer the use of ;s too:

Yeah, makes it more "readable"
>String.prototype.LTrim =
function() {
return this.replace(/^\s+/, '');
};

Changed. The names are also changed:

String.prototype.lTrim=
function()
{
return this.replace(/^\s+/,'');
}
String.prototype.lTrim=
function()
{
return this.replace(/\s+$/,'');
}
String.prototype.trim=
function()
{
return this.replace(/^\s+|\s+$/g,'');
}
Could we have a space before the = too?

String.prototype.trim =

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Feb 26 '07 #26
Evertjan. said the following on 2/26/2007 2:14 AM:
Randy Webb wrote on 26 feb 2007 in comp.lang.javascript:
>Evertjan. said the following on 2/25/2007 4:27 PM:
>>Randy Webb wrote on 25 feb 2007 in comp.lang.javascript:
[...]
>>It makes demo code more easily readable, IMHO.
I don't agree with the "more easily readable"

I ment for a newbee, as the FAQ is most useful to.
Later on it seems handy to compact these library-like functions
as not to clutter the "real" programming.
Very true.

<snip>
Could we have a space before the = too?

String.prototype.trim =
Done locally.

--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq/index.html
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Feb 26 '07 #27
In comp.lang.javascript message <of********************@telcove.net>,
Sun, 25 Feb 2007 20:12:58, Randy Webb <Hi************@aol.composted:
>
String.prototype.lTrim=
function()
{
return this.replace(/^\s+/,'');
}
String.prototype.lTrim=
function()
{
return this.replace(/\s+$/,'');
}
String.prototype.trim=
function()
{
return this.replace(/^\s+|\s+$/g,'');
}

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

etc.

Personally, I see no need for isolating the braces, though I know others
like it. To me it smacks of pay-by-the-yard coding, a practice
doubtless unknown in mainland Europe.

--
(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.
Proper <= 4-line sig. separator as above, a line exactly "-- " (SonOfRFC1036)
Do not Mail News to me. Before a reply, quote with ">" or "" (SonOfRFC1036)
Feb 26 '07 #28

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

31
by: rkk | last post by:
Hi, I've written a small trim function to trim away the whitespaces in a given string. It works well with solaris forte cc compiler, but on mingw/cygwin gcc it isn't. Here is the code: char...
12
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - How do I trim whitespace - LTRIM/RTRIM/TRIM?...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.