JavaScript Document (8) Events (8) ExtJS (9) Strings (3)
Exchange Links About this site Links to us 
|
How can I trim a string in JavaScript?
43 comments. Current rating: (15 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 2 of 3, other pages: 1 [2] 3 | 2008-06-19, 09:45:29 (updated: 2008-06-19, 09:46:23) |
|
|
|
Very useful. Who would have thunk that Javascript doesn't have an inbuilt trim function? I always assumed it did...
|
|
|
|
|
function trim(str)
{
var newStr='';;
for(i=0;i<str.length;i++)
{
if(str[i]!=' ')
{
newStr+=str[i];
}
}
return newStr;
}
|
2008-07-08, 12:21:12 (updated: 2008-07-08, 12:55:07) |
|
|
|
Its slightly faster to combine the regular expressions into a single one. In fact its even faster to make this a standalone function into of being a prototype on String but if you want to keep it that way, here it is:
String.prototype.trim = function() {
return this.replace(/^\s+|\s+$/g,'');
}
(If anyone wants benchmarks let me know)
Please remove the picture... I thought it would be a gravatar or something...
|
2008-08-15, 16:35:07 (updated: 2008-08-15, 16:36:05) |
|
|
|
Regular expression is fine.
Otherwise, the fastest I have found is this one:
function trimString(s)
{
if ((s == null)||(s.length == 0))
return s;
for(var start = 0; s.charCodeAt(start) == 32; start++)
if (start == s.length-1)
return '';
for(var end = s.length-1; s.charCodeAt(end) == 32; end--);
return s.substring(start, end+1);
}
|
|
|
|
|
I did some benchmark with ie 7.0 and ff 3.0. (ff is much faster)
reg exp is faster when we remove at least:
In IE: 6 Spaces
In FF: 20 Spaces
Otherwise, trimString(s) by hand is faster.
|
|
|
|
|
Gr8
|
|
|
|
hi
I am searching information on string trimmers and I have visited one site http://www.sharperblade.com. If anybody knowing about string trimmers and you spot any issues that need to be fixed and or improved please let me know as soon as possible.
thank you
|
|
|
|
|
Thank you very much saved some of my time...
|
|
|
|
Notice that the regular expression posted by joe:
String.prototype.trim = function() {
return this.replace(/^\s+|\s+$/g,'');
} |
is the same than the one used by Microsoft in their ajax API:
function String$trim()
{
/// <summary locid='M:J#String.trim' />
/// <returns type='String'></returns>
if (arguments.length !== 0) throw Error.parameterCount();
return this.replace(/^\s+|\s+$/g, '');
}
|
|
|
|
|
Hello ,
I have a string like 'Error:Error: Hello World calling service createHelloWorld in createUpdateHelloWorld' I want only Hello world and have to remove Error:Error: calling service createHelloWorld in creatUpdateHelloWorld .......how can I do this.
Any help is much appreciated.
|
|
|
|
|
Good one thanks
|
|
|
|
|
Hi,
I want to trim <p>text </p> it to as text in javascript.
Could anyone help out me in uregently.
Send your reply to rajuavs82@yahoo.com or rajuavs82@gmail.com
Thanks in Advance.
Regards,
Raju.
|
|
|
|
|
7550 7550 7550 7550 7550 7550 7550 7550 7550
|
|
|
|
|
|
| You are on page 2 of 3, other pages: 1 [2] 3 |
|