DelphiFAQ Home Search:

How can I trim a string in JavaScript?

 

comments21 comments. Current rating: 5 stars (7 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 2, other pages: [1] 2
2007-04-05, 13:22:57
anonymous from United States  
Super. Thank you. Learnt something today :-)
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-05-28, 01:58:20
samuel.baktiar@yahoo.com from Indonesia  
that's elegant way, thk.
2007-06-20, 13:11:16
anonymous from United States  
Beautiful! Haven't seen any code more compact than this. Thank you.
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-08-05, 18:16:25
jaduboy@go.com from United States  
Your function helped me remove some extra whitespace while using ajax. Thanks for your help.

Aamir Ali
2007-09-24, 08:17:24
anonymous from Chicago, United States  
The funtions did not work, but thank you for the replace function, it helped me to remove the end of string white space in my ajax.
2007-10-03, 05:58:22   (updated: 2007-10-03, 05:59:56)
anonymous from India  
Thanks for giving this trim function
Raviiiii......
2007-10-03, 15:00:54
anonymous from United States  
Remarkably 1337, thank you.
2007-11-06, 06:52:58
anonymous from Dominican Republic  
GOOOODDD
2007-11-23, 00:45:47
neel from United States  
rating
too good
2007-12-05, 00:39:38
anonymous from India  
rating
Cool
2007-12-20, 00:17:03
bk from India  
rating
Thanks for giving this trim function
2007-12-21, 08:55:12
anonymous from United States  
thanx a lot afzal
2008-01-23, 02:57:12
anonymous from India  
when i import a email in the to: box i get a email of format:
'Amey Nevrekar' <tommyhighfier@aol.com>

I want it in the format: tommyhighfier@aol.com

I want to remove all other characters and quotes and the <> from the string.

Can anybody help urgently?

send your reply to ameynevrekar@yahoo.co.in

Thank You
Amey
You are on page 1 of 2, other pages: [1] 2

 

 

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.
 
It seems that you are
from Washington, US .

Info/ Feedback on this

Show city and country
Show country only
Hide my location
Leave your comment here:
Please type in the code:
photo Add a picture:

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.