DelphiFAQ Home Search:

How can I trim a string in JavaScript?

 

comments66 comments. Current rating: 5 stars (26 votes). Leave comments and/ or rate it.

Question:

I need to trim a string from white space at the front and end. How can I do that elegantly?

Answer:

Add the following string prototype function to your code. You will call it like predefined String member functions (such
as indexOf(), charAt() and Substring().

The code below shows the function declaration and how to use it.

This implementation of a trim() function uses two regular expressions to first replace the white space at the begiinning,
then the white space at the end of the string. White space in regular expressions is described as \s.
The beginning of the string is matched by ^ (see the first regex) and the end is matched by $ - in the second regex.

// implementing a trim function for strings in javascript

<script language="JavaScript" type="text/javascript">
<!--
String.prototype.trim = function () {
    return this.replace(/^\s*/, "").replace(/\s*$/, "");
}

var s = new String(" Hello ");
// use it like this
s=s.trim();
alert("!" + s + "!");

// end hiding contents -->
</script>


Comments:

You are on page 1 of 5, other pages: [1] 2 3 4 5
2007-05-19, 02:15:14
[hidden] from Pakistan  
function trim(s)
{
    var l=0; var r=s.length -1;
    while(l < s.length && s[l] == ' ')
    {     l++; }
    while(r > l && s[r] == ' ')
    {     r-=1;     }
    return s.substring(l, r+1);
}
2007-07-16, 01:04:39
anonymous from India  
for the code mentioned by [hidden] from Pakistan, i had seen that s[l] is actually undefined and it does not give any valid character if we use s[l] .

so better use the following code:

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


-Afzal Aziz
2007-10-03, 05:58:22   (updated: 2007-10-03, 05:59:56)
anonymous from India  
Thanks for giving this trim function
Raviiiii......
2007-11-06, 06:52:58
anonymous from Dominican Republic  
GOOOODDD
2007-12-05, 00:39:38
anonymous from India  
rating
Cool
2008-05-20, 10:44:59   (updated: 2008-05-20, 10:46:28)
anonymous from United States  
rating
Great function !!!! Just copied, pasted and used it. THANKS !!!
2008-06-09, 04:27:14
anonymous from Indonesia  
rating
Very helpful, thaks..
2008-06-17, 10:56:02
anonymous from United States  
rating
Gr8 compact function thanks
You are on page 1 of 5, other pages: [1] 2 3 4 5

 

 

NEW: Optional: Register   Login
Email address (not necessary):

Rate as
Hide my email when showing my comment.
Please notify me once a day about new comments on this topic.
Please provide a valid email address if you select this option, or post under a registered account.
 

Show city and country
Show country only
Hide my location
You can mark text as 'quoted' by putting [quote] .. [/quote] around it.
Please type in the code:

Please do not post inappropriate pictures. Inappropriate pictures include pictures of minors and nudity.
The owner of this web site reserves the right to delete such material.

photo Add a picture: