Go4Expert

Go4Expert (http://www.go4expert.com/)
-   JavaScript and AJAX (http://www.go4expert.com/articles/javascript-tutorials/)
-   -   Trim function JavaScript/JScript (http://www.go4expert.com/articles/trim-function-javascript-jscript-t2098/)

pradeep 1Dec2006 18:02

Trim function JavaScript/JScript
 
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,}/,' ');
}


Parimal 4May2007 10:22

Re: Trim function JavaScript/JScript
 
great

urstop 28Nov2007 11:48

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.

prasanna_gg 10Mar2009 08:16

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)!

gkumar 23Jun2009 16:03

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)+"*");


pradeep 24Jun2009 10:20

Re: Trim function JavaScript/JScript
 
Nice one gkumar

dubeyparam 2Jul2010 15:23

Re: Trim function JavaScript/JScript
 
Good Job

Full Zip Hoody 30Sep2010 01:32

Re: Trim function JavaScript/JScript
 
it works fine for me both ways. the most important thing is that there is a way of doing this without pretty much effort at all.


All times are GMT +5.5. The time now is 19:53.