Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > How can I trim the sides of a string?

Reply
Thread Tools

How can I trim the sides of a string?

 
 
Reply Via Newsgroup
Guest
Posts: n/a
 
      04-05-2004

Folks,

In PHP and some other scripting languages, one has trim() - It removes
newline, tabs and blank spaces that might prefix, or suffix a string.

Can someone tell me how I can do this in javascript?

Much appreciated,
randell d.
 
Reply With Quote
 
 
 
 
Janwillem Borleffs
Guest
Posts: n/a
 
      04-05-2004
Reply Via Newsgroup wrote:
> In PHP and some other scripting languages, one has trim() - It removes
> newline, tabs and blank spaces that might prefix, or suffix a string.
>
> Can someone tell me how I can do this in javascript?
>


http://jibbering.com/faq/#FAQ4_16


JW



 
Reply With Quote
 
 
 
 
pcx99
Guest
Posts: n/a
 
      05-01-2004
Reply Via Newsgroup wrote:
>
> Folks,
>
> In PHP and some other scripting languages, one has trim() - It removes
> newline, tabs and blank spaces that might prefix, or suffix a string.
>
> Can someone tell me how I can do this in javascript?
>
> Much appreciated,
> randell d.



http://www.vermontsoftware.com/Javascript/trim.html
 
Reply With Quote
 
Fox
Guest
Posts: n/a
 
      05-02-2004


pcx99 wrote:
>
> Reply Via Newsgroup wrote:
> >
> > Folks,
> >
> > In PHP and some other scripting languages, one has trim() - It removes
> > newline, tabs and blank spaces that might prefix, or suffix a string.
> >
> > Can someone tell me how I can do this in javascript?
> >
> > Much appreciated,
> > randell d.

>
> http://www.vermontsoftware.com/Javascript/trim.html



String.prototype.trim = function()
{
return this.match(/(\b\S+|\S+)/g).join(" ");
}
 
Reply With Quote
 
Michael Winter
Guest
Posts: n/a
 
      05-02-2004
On Sun, 02 May 2004 07:03:20 -0500, Fox <(E-Mail Removed)> wrote:

[snip]

> String.prototype.trim = function()
> {
> return this.match(/(\b\S+|\S+)/g).join(" ");
> }


The FAQ version seems to be more logical.

Mike

--
Michael Winter
http://www.velocityreviews.com/forums/(E-Mail Removed)d (replace ".invalid" with ".uk" to reply)
 
Reply With Quote
 
Dr John Stockton
Guest
Posts: n/a
 
      05-04-2004
JRS: In article <(E-Mail Removed)>, seen in
news:comp.lang.javascript, Michael Winter <(E-Mail Removed)
d> posted at Sun, 2 May 2004 15:52:03 :
>On Sun, 02 May 2004 07:03:20 -0500, Fox <(E-Mail Removed)> wrote:
>
>[snip]
>
>> String.prototype.trim = function()
>> {
>> return this.match(/(\b\S+|\S+)/g).join(" ");
>> }

>
>The FAQ version seems to be more logical.


More importantly, it does what "randell d." asked for, whereas the
quoted code also converts multiple spaces within the string to singles.

That task can, AFAICS, be done by S.split(/\s+/).join(" "); and that
task might be worth including in the FAQ section, with optimum solution.

--
John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4
<URL:http://jibbering.com/faq/> Jim Ley's FAQ for news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
 
Reply With Quote
 
rh
Guest
Posts: n/a
 
      05-05-2004
Dr John Stockton <(E-Mail Removed)> wrote in message news:<(E-Mail Removed)>...
> JRS: In article <(E-Mail Removed)>, seen in
> news:comp.lang.javascript, Michael Winter <(E-Mail Removed)
> d> posted at Sun, 2 May 2004 15:52:03 :
> >On Sun, 02 May 2004 07:03:20 -0500, Fox <(E-Mail Removed)> wrote:
> >
> >[snip]
> >
> >> String.prototype.trim = function()
> >> {
> >> return this.match(/(\b\S+|\S+)/g).join(" ");
> >> }

> >
> >The FAQ version seems to be more logical.

>
> More importantly, it does what "randell d." asked for, whereas the
> quoted code also converts multiple spaces within the string to singles.
>
> That task can, AFAICS, be done by S.split(/\s+/).join(" "); and that
> task might be worth including in the FAQ section, with optimum solution.


The FAQ version is clearly the correct way to trim strings. However,
I'm curious as to the reason the FAQ prototype is generated with "new
Function(str)" instead of the more common function-expression form
given above.

Strings introduce the requirement for double escaping the "\" (as per
new RegExp), and while such use may be instructive, it adds
chicken-track complexity where in a place where it doesn't appear to
be necessary.

Am I missing some subletly regarding browser compatibility, or
something to do with "this" association?

../rh
 
Reply With Quote
 
Shawn Milo
Guest
Posts: n/a
 
      05-05-2004
Reply Via Newsgroup <(E-Mail Removed)> wrote in message news:<FMjcc.30509$Pk3.9550@pd7tw1no>...
> Folks,
>
> In PHP and some other scripting languages, one has trim() - It removes
> newline, tabs and blank spaces that might prefix, or suffix a string.
>
> Can someone tell me how I can do this in javascript?
>
> Much appreciated,
> randell d.


There is a quick, easy way to do this with a regular expression.
It only takes one line of Javascript.

Code written, commented, and tested by me. No warranty. )

Shawn




<script type="text/javascript">


var testString = ' there are leading and trailing spaces (and tabs) ';

alert('\'' + testString + '\'');


//regex explanation:
//regex is in forward slashes: //
// syntax string = string.replace(/pattern/, 'replace with this');

^ = beginning of line
(\s+)? = one or more characters of whitespace, optional
(.*\S) = any characters, with the last one not being whitespace
(\s+)? = one or more characters of whitespace, optional
$ = end of line
$2 = what was in the 2nd set of parenthesis

testString = testString.replace(/^(\s+)?(.*\S)(\s+)?$/, '$2');

alert('\'' + testString + '\'');

</script>
 
Reply With Quote
 
Thomas 'PointedEars' Lahn
Guest
Posts: n/a
 
      05-06-2004
rh wrote:

>>> Fox <(E-Mail Removed)> wrote:
>>>> [...]
>>>> String.prototype.trim = function()
>>>> {
>>>> return this.match(/(\b\S+|\S+)/g).join(" ");
>>>> }

>
> The FAQ version is clearly the correct way to trim strings. However,
> I'm curious as to the reason the FAQ prototype is generated with "new
> Function(str)" instead of the more common function-expression form
> given above.
>
> Strings introduce the requirement for double escaping the "\" (as per
> new RegExp), and while such use may be instructive, it adds
> chicken-track complexity where in a place where it doesn't appear to
> be necessary.


Depends.

> Am I missing some subletly regarding browser compatibility,


Yes, indeed you do. If Netscape's Core JavaScript Reference 1.5 and the
MSDN Library is to be trusted, the Function() constructor is available from
JavaScript 1.1 (NN3+) and JScript 2.0 (IE/IIS 3+) on, while the "function"
statement within another statement (including itself) or the "function"
operator (also defining anonymous functions) requires JavaScript 1.2 (NN4+)
or 1.5 (NN6+; in JScript AFAIS both are available in all versions and thus
all UAs), respectively. (Unfortunately, the Reference cannot be trusted
here since I had to learn that NN 4.7 which claims to support JavaScript
up to 1.3 only also supports the "function" operator.)

As for the ECMAScript standard, the "function" statement within a
statement is AFAIS not available before edition 3 as there is no
FunctionExpression to be produceable through the MemberExpression production.

While the UAs given respect to herewith may seem outdated, there may
be still UAs around which implement only those older versions of the
respective language(s). However, we also had one case of a buggy
implementation here that had b0rken Function() constructor support.
Google is your friend. [psf 6.1]

<http://devedge.netscape.com/library/manuals/2000/javascript/1.5/reference/function.html#1193137>
<http://devedge.netscape.com/library/manuals/2000/javascript/1.5/reference/stmt.html#1004825>
<http://devedge.netscape.com/library/manuals/2000/javascript/1.5/reference/ops.html#1066344>
<http://msdn.microsoft.com/library/default.asp?url=/library/en-us/script56/html/js56jsoriVersionInformation.asp>

> or something to do with "this" association?


No.

String.prototype.trim = new Function(
"return this.match(/(\\b\\S+|\\S+)/g).join(' ');");

is semantically equal to the above.


HTH

PointedEars
 
Reply With Quote
 
Jon Kelling
Guest
Posts: n/a
 
      05-06-2004
for all your javascript trimming needs...

function trim(inputString) {
if (typeof inputString != "string") return inputString;
return inputString
//clear leading spaces and empty lines
.replace(/^(\s|\n|\r)*((.|\n|\r)*?)(\s|\n|\r)*$/g,"$2")

//take consecutive spaces down to one
.replace(/(\s(?!(\n|\r))(?=\s))+/g,"")

//take consecutive lines breaks down to one
.replace(/(\n|\r)+/g,"\n\r")

//remove spacing at the beginning of a line
.replace(/(\n|\r)\s/g,"$1")

//remove spacing at the end of a line
.replace(/\s(\n|\r)/g,"$1");
}

-- Jon

pcx99 <(E-Mail Removed)> wrote in message news:<egVkc.5975$(E-Mail Removed) m>...
> Reply Via Newsgroup wrote:
> >
> > Folks,
> >
> > In PHP and some other scripting languages, one has trim() - It removes
> > newline, tabs and blank spaces that might prefix, or suffix a string.
> >
> > Can someone tell me how I can do this in javascript?
> >
> > Much appreciated,
> > randell d.

>
>
> http://www.vermontsoftware.com/Javascript/trim.html

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
FAQ Topic - How do I trim whitespace - LTRIM/RTRIM/TRIM? FAQ server Javascript 26 02-26-2007 05:06 PM
FAQ Topic - How do I trim whitespace - LTRIM/RTRIM/TRIM? FAQ server Javascript 6 12-25-2006 08:47 PM
FAQ Topic - How do I trim whitespace - LTRIM/RTRIM/TRIM? FAQ server Javascript 0 10-25-2006 11:00 PM
FAQ Topic - How do I trim whitespace - LTRIM/RTRIM/TRIM? FAQ server Javascript 0 08-28-2006 11:00 PM



Advertisments