JavaScript Document (8) Events (8) Strings (3)
Exchange Links About this site Links to us
New related comments Number of comments in the last 48 hoursHow can I trim a string in JavaScript? 1 new comments
|
How can I trim a string in JavaScript?
21 comments. Current rating: (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.
 | |  | |
<script language="JavaScript" type="text/javascript">
<!--
String.prototype.trim = function () {
return this.replace(/^\s*/, "").replace(/\s*$/, "");
}
var s = new String(" Hello ");
s=s.trim();
alert("!" + s + "!");
</script>
| |  | |  |
Comments:
| You are on page 1 of 2, other pages: [1] 2 | |
|
|
|
Super. Thank you. Learnt something today :-)
|
|
|
|
|
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);
}
|
|
|
|
|
that's elegant way, thk.
|
|
|
|
|
Beautiful! Haven't seen any code more compact than this. Thank you.
|
|
|
|
|
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
|
|
|
|
|
Your function helped me remove some extra whitespace while using ajax. Thanks for your help.
Aamir Ali
|
|
|
|
|
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) |
|
|
|
Thanks for giving this trim function
Raviiiii......
|
|
|
|
|
Remarkably 1337, thank you.
|
|
|
|
|
GOOOODDD
|
|
|
|
|
too good
|
|
|
|
|
Cool
|
|
|
|
|
Thanks for giving this trim function
|
|
|
|
|
thanx a lot afzal
|
|
|
|
|
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 |
|