Learn how to Make Money Online | Free Tech Magazines
Go4Expert
Go4Expert RSS Feed

Go Back   Programming and SEO Forum >  Go4Expert > Articles / Source Code > Web Development > HTML/DHTML - JavaScript/VBScript

Discuss / Comment Copy HTML to Clipboard  Copy BBCode to Clipboard  Add to del.icio.us  Add to Google  Digg it  Add to Yahoo !  Add to Windows Live  Add to Facebook  Add to StumbleUpon 
 
Bookmarks Article Tools Search this Article Display Modes

Trim function JavaScript/JScript

By pradeep pradeep is offline

On 1st December, 2006
Trim function JavaScript/JScript

ADVERTISEMENT
Show Printable Version Email this Page Subscription Add to Favorites Copy Trim function JavaScript/JScript link

Author

pradeep ( Team Leader )

Yet to provide details about himself


All articles By pradeep

Recent Articles

Similar Articles

I needed to trim a string and also replace more than one spaces with a single space. The solution was simple with String.replace in JavaScript. Here's the code:

Code: JavaScript
var m = " My name  is Pradeep ";
m = m.replace(/^[\s]+/,'').replace(/[\s]+$/,'').replace(/[\s]{2,}/,' ');
Making a function out of it.
Code: JavaScript
function trim(str)
{
    if(!str || typeof str != 'string')
        return null;

    return str.replace(/^[\s]+/,'').replace(/[\s]+$/,'').replace(/[\s]{2,}/,' ');
}
Old 05-04-2007, 11:22 AM   #2
Parimal
Newbie Member
 
Join Date: May 2007
Location: pagalkhana
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Power: 0
Parimal is on a distinguished road
Smile

Re: Trim function JavaScript/JScript


great
Parimal is offline   Reply With Quote
Old 11-28-2007, 11:48 AM   #3
urstop
Go4Expert Member
 
Join Date: Oct 2007
Posts: 43
Thanks: 0
Thanked 1 Time in 1 Post
Rep Power: 0
urstop is on a distinguished road

Re: Trim function JavaScript/JScript


Good use of regular expressions, we wish there was a simpler method like vbscript where in we can just use the function trim.
__________________
Regards,
AV
Freelance Software Services UK
urstop is offline   Reply With Quote
Old 03-10-2009, 09:16 AM   #4
prasanna_gg
Newbie Member
 
Join Date: Mar 2009
Location: Melbourne, Australia
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Power: 0
prasanna_gg is on a distinguished road

Re: Trim function JavaScript/JScript


Hi Pradeep,

Nice solution. Although it could be extended nicely so it works natively in Javascript. JS provides a way to create prototype functions easily.

Here is the code:

Code:
String.prototype.trim = function() {  return this.replace(/^\s+|\s+$/g, '');  }
This method can be called like any in-built javascript function - string.trim() instead of trim(string)!
prasanna_gg is offline   Reply With Quote
Old 06-23-2009, 05:03 PM   #5
gkumar
Banned
 
Join Date: Jun 2009
Posts: 58
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Power: 0
gkumar will become famous soon enough

Re: Trim function JavaScript/JScript


Use the code below to make trim a method of all Strings. These are useful to place in a global Javascript file included by all your pages.

Code:
String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g,"");
}
String.prototype.ltrim = function() {
	return this.replace(/^\s+/,"");
}
String.prototype.rtrim = function() {
	return this.replace(/\s+$/,"");
}

// example of using trim, ltrim, and rtrim
var myString = " hello my name is ";
alert("*"+myString.trim()+"*");
alert("*"+myString.ltrim()+"*");
alert("*"+myString.rtrim()+"*");
Javascript Trim Stand-Alone Functions:-If you prefer not to modify the string prototype, then you can use the stand-alone functions below.

Code:
function trim(stringToTrim) {
	return stringToTrim.replace(/^\s+|\s+$/g,"");
}
function ltrim(stringToTrim) {
	return stringToTrim.replace(/^\s+/,"");
}
function rtrim(stringToTrim) {
	return stringToTrim.replace(/\s+$/,"");
}

// example of using trim, ltrim, and rtrim
var myString = " hello my name is ";
alert("*"+trim(myString)+"*");
alert("*"+ltrim(myString)+"*");
alert("*"+rtrim(myString)+"*");
gkumar is offline   Reply With Quote
Old 06-24-2009, 11:20 AM   #6
pradeep
Team Leader
 
pradeep's Avatar
 
Join Date: Apr 2005
Location: Kolkata, India
Posts: 1,461
Thanks: 0
Thanked 19 Times in 16 Posts
Rep Power: 6
pradeep will become famous soon enough
Send a message via Yahoo to pradeep

Re: Trim function JavaScript/JScript


Nice one gkumar
__________________
Vote for the Most Entertaining Member of 2008

To err is human,to detect is divine!
pradeep is offline   Reply With Quote
Discuss / Comment Copy HTML to Clipboard  Copy BBCode to Clipboard  Add to del.icio.us  Add to Google  Digg it  Add to Yahoo !  Add to Windows Live  Add to Facebook  Add to StumbleUpon 


Currently Active Users Reading This Article: 1 (0 members and 1 guests)
 
Article Tools Search this Article
Search this Article:

Advanced Search
Display Modes
Bookmarks

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
Forum Jump

Similar Threads / Articles
Thread Thread Starter Forum Replies Last Post
100 Multiple choice questions in C coderzone C-C++ 98 08-26-2009 12:17 AM
Circular linked list shabbir C-C++ 36 07-08-2009 01:12 AM
Double circular linked list shabbir C-C++ 8 06-01-2009 01:18 PM
Calling Export function in Exe from MFC Function naveen1anand MFC / Win32 0 11-11-2006 10:04 PM
function passing not returning proper value musicmancanora4 C-C++ 2 03-17-2006 07:15 PM

 

All times are GMT +5.5. The time now is 04:24 AM.