C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Communities   Interview   Jobs   Projects   Offshore Development    
Silverlight Tutorials | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Revenue Sharing |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...

New Feature: Community Sites: Create your own .NET community website and start earning from Google AdSense ! It's Free !






how to use trim() in javascript


Posted Date: 30 Oct 2008    Resource Type: Code Snippets    Category: Javascript
Author: abhay kumar routMember Level: Gold    
Rating: Points: 7



In programming, trim (or strip) is a common string manipulation function which removes leading and trailing whitespace from a string. Precisely what constitutes whitespace varies between programming languages and implementations, but normally includes space, tab, line feed, and carriage return characters.



Using Loops:

function trim(str) {
while (str.substring(0, 1) == ' ') {
str = str.substring(1, str.length);
}
while (str.substring(str.length - 1, str.length) == ' ') {
str = str.substring(0, str.length - 1);
}
return str;
}

Usage:
var str = ' abc ';
var trimmedStr = trim(str);

OR

Using Regular Expressions

function trim(str) {
return str.replace(/^\s+|\s+$/g,'');
}

Usage:
var str = ' abc ';
var trimmedStr = trim(str);



OR

Using an Object Oriented Approach


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

Usage:
var str = ' abc '; // OR var str = new String(' abc ');
var trimmedStr = str.trim();






Responses


No responses found. Be the first to respond and make money from revenue sharing program.

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Trim  .  

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Loading Progress Graphically using Javascript
Previous Resource: How to call the parent page javascript function from a popup window
Return to Discussion Resource Index
Post New Resource
Category: Javascript


Post resources and earn money!
 
Related Resources



dotNet Slackers   BizTalk Adaptors    Web Design


Contact Us    Privacy Policy    Terms Of Use