Learn how to Make Money Online doing freelancing, Affiliate Marketing, Blogging and many more ...
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  | More
 
Bookmarks Article Tools Search this Article Display Modes

Trim function JavaScript/JScript


On 1st December, 2006
Trim function JavaScript/JScript

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, 10:22 AM   #2
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, 10:48 AM   #3
Contributor
 
Join Date: Oct 2007
Posts: 84
Thanks: 0
Thanked 2 Times in 2 Posts
Rep Power: 3
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, 08:16 AM   #4
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, 04:03 PM   #5
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, 10:20 AM   #6
Team Leader
 
pradeep's Avatar
 
Join Date: Apr 2005
Location: Kolkata, India
Posts: 1,470
Thanks: 0
Thanked 35 Times in 30 Posts
Rep Power: 7
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
Old 07-02-2010, 03:23 PM   #7
Newbie Member
 
Join Date: Jul 2010
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Power: 0
dubeyparam is on a distinguished road

Re: Trim function JavaScript/JScript


Good Job
dubeyparam is offline   Reply With Quote
Discuss / Comment  Copy HTML to Clipboard  Copy BBCode to Clipboard  | More


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
Trackbacks are Off
Pingbacks are Off
Refbacks are Off

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

 

All times are GMT +5.5. The time now is 05:25 AM.