Skip Navigation
Details
Author: Jake Howlett
Date: Mon 13 Mar 2006

Permalink

Comments / Add / Subscribe

Elsewhere

25 Best License-Free Quality Fonts
Mon 13 Mar 2006 [alvit.de]

A library of nice looking DHTML scripts
Mon 13 Mar 2006 [dhtmlgoodies.com]

More links are available
in the archive »

« Tips Week 2.2 - Trimming Names in LotusScript | Blogs | Cross-Domain Ajax on Domino »

Tips Week 2.1 - JavaScript Trim() Function

I'm going to be too busy this week to blog anything insightful, so I'll do another tips week. Starting today with a little extension to the JavaScript String object.

Add this function to a JS library somewhere:

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

Then you can call is from wherever you like, as so:

alert(" test" + " this ".trim() )

The result of which is a prompt showing " test this". Or use like this:

<input type="test" onchange="this.value=this.value.trim()" >

It's a useful function. Don't leave home without it.

More String extensions here. More tips each day this week.

Comments

Dragon Cotterill (Mon 13 Mar 2006 06:48 AM) website / e-mail

Ummm. Is that a typo? Surely the alert above should return " testthis".

You could shorten this to something like;

return this.replace(/^\s*(\b.*\b|)\s*$/, '');

which will remove leading/trailing whitespaces. But does anybody have anything which will act like @Trim and cut out whitespaces in the middle of strings?

Jake (Mon 13 Mar 2006 07:05 AM)

Woops. Yes. It is and it will.

Dragon Cotterill (Mon 13 Mar 2006 07:06 AM) website / e-mail

Ah. Here we go. A Regexp which will remove repeated whitespaces and replace with a single space.

return this.replace(/^\s*|\s(?=\s)|\s*$/g, "");

Not perfect (turns tabs into spaces) but close enough for me.

John Z Marshall (Mon 13 Mar 2006 07:14 AM) website / e-mail

Trim is always useful. That's why I have these:

String.prototype.trim = function() {

return this.replace(/(?:(?:^|\n)\s+|\s+(?:$|\n))/g,"");

}

String.prototype.fulltrim = function() {

return this.replace(/(?:(?:^|\n)\s+|\s+(?:$|\n))/g,"").replace(/\s+/g," ");

}

Not sure whether they bring anything else to the party. But I have been using them for a long while and hand no complaints, yet.

Vince (Mon 13 Mar 2006 08:03 AM) website / e-mail

A couple other handy ones include:

String.prototype.right = function(count) {

return this.substr(this.length - count);

}

String.prototype.left = function(count) {

return this.substr(0, count);

}

Ron Yuen (Tue 14 Mar 2006 04:02 AM)

Duplicating Lotusscript/@Formula functions in Javascript is frequently useful, I have found the @Unique(list) capability especiually so.

here is a JS version :

Array.prototype.indexOf = function(str,ignoreCase) {

// return the index of the first instance of str in the array

// else -1 if not a member

// if a string array can take a flag to request case match

// if (ignoreCase) then 'StringA' is considered the same as 'sTrinGa'

var tmp ;

if (typeof(str)=="string" && ignoreCase) {

str = str.toLowerCase() ;

}

for (var i=0; i<this.length; i++) {

if (typeof(str)=="string" && ignoreCase) {

tmp=this[i].toLowerCase() ;

} else {

tmp=this[i] ;

}

if (tmp==str) return i ;

}

return -1 ;

}

Array.prototype.isMember = function(str,ignoreCase) {

return this.indexOf(str,ignoreCase) > -1 ;

}

Array.prototype.unique = function(ignoreCase) {

// creates a unique array

// empty arrays are always unique !

if (this.length==0) return this ;

// default to respect case

// if (ignoreCase) then 'StringA' is considered the same as 'sTrinGa'

if (arguments.length==0) ignoreCase = false ;

var newList = new Array(this[0]) ;

var found,tmp1,tmp2 ;

for (var i=0; i<this.length; i++) {

if (!newList.isMember(this[i],ignoreCase)) newList.push(this[i]) ;

}

return newList ;

}

To be really bulletproof you may want to check that the browser supports array.push() and if not then implement one !

Regards

Ron Yuen

Ady (Wed 17 May 2006 01:56 PM) e-mail

Hi Jake et al,

Anyone know how 'fully' @Trim an html textarea?

e.g. create an editable text field with, say 5 rows and 20 cols, and allow multiple values. Then populate the text area with just spaces and carriage returns.

If you put @Trim(@ThisValue) in the Input Translation and submit the form then all the 'whitespace' is removed, but there doesn't seem to be a way to do this in javascript prior to submitting the form...

Any ideas anyone?!

Robert (Mon 14 Aug 2006 08:34 PM)

//*** removes whitespace fom begining and end of a string and returns it to whereever.

function trim(string){

if(string.indexOf(" ") == 0){

while(string.indexOf(" ") < 1){

string = string.substring(1,string.length) ;

}

}

if(string.substring(string.length-1,string.length) == " "){

while(string.substring(string.length-1,string.length) == " "){

string = string.substring(0,string.length-1) ;

}

}

return string;

}

function yourFunction(str){

....

str = trim(str);

....

}

Jorge (Mon 25 Sep 2006 07:32 AM) website / e-mail

Thank you very much.

This little func was just what i was looking for.

Keep it up !

Pete (Fri 5 Jan 2007 06:24 AM)

Robert's code is flawed. If the string has leading spaces but no trailing spaces then it will get caught in an infinite loop.

Here's a version that will allow for leading spaces only, trailing spaces only, both leading and trailing spaces and no leading or trailing spaces.

function trim(string){

' remove leading spaces

while(string.substr(0,1)==" ")

string = string.substring(1,string.length) ;

' remove trailing spaces

while(string.substr(string.length-1,1)==" ")

string = string.substring(0,string.length-2) ;

return string;

}

Sowmya (Thu 8 Mar 2007 03:31 AM) e-mail

I need the following output:

I have a variable in which the value it has is:

" 1 : 3300"

I need to trim it to 3300 and store it as an integer.

How is it possible?

Please do reply.

Thank you,

Regards,

Sowmya,

Bangalore, India

Rochak Chauhan (Wed 17 Oct 2007 12:44 PM) website / e-mail

There are many required functions which are not available in Native JavaScript.Get trim and more functions like trim() for JavaScript:

{Link}

Russell (Thu 14 Feb 2008 11:34 PM)

Sowmya ,

use trim function...

smthing like

strObj.trim(":")[1]

Ryan (Wed 21 May 2008 09:37 AM) website / e-mail

Have a look at this article for more ways of implementing javascript trim: {Link}

Add your response here:

Name *:
E-mail:
Protected from spambots!
Website:
rel="nofollow"

Comment *:
HTML is not allowed!

Note: This blog entry is more than two weeks old so your comment, as an anti-spam measure, will be sent for approval.