Click here to Skip to main content
11,804,008 members (74,663 online)
Click here to Skip to main content

Create Trim function in javascript

, 13 Nov 2010 CPOL 13.8K 5
Rate this:
Please Sign up or sign in to vote.
You can define a trim function which trims leading and trailing space in JavaScript by using any one of the following ways:

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

way 2:
function trim()
 return this.replace(/^\s+|\s+$/g, ''); 

The following function shows how to use the trim function

function temp()
 var str='    maninder     ';

You will see 'maninder' in the alert box. All the leading and trailing space are trimmed. Smile | :)


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Software Developer
India India
No Biography provided

You may also be interested in...

Comments and Discussions

GeneralPlease rate this if it helped you Pin
wadhva.maninder2-Nov-10 4:42
memberwadhva.maninder2-Nov-10 4:42 
General\s is whitespace Pin
jsc428-Nov-10 22:59
memberjsc428-Nov-10 22:59 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.151002.1 | Last Updated 13 Nov 2010
Article Copyright 2010 by shankeyshankey
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid