Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

trim a string?

Status
Not open for further replies.

markshen2004

Programmer
Jun 9, 2004
89
CA
I have a tring with space.it is like this:

string1=" test ";

I want to take out the whitespace in left site and whitespace in right site .

Please give me a idea how to do it.Thanks
 
This should do it:

[tt]string1 = string1.replace(/^\s*|\s*$/g,"");[/tt]

*cLFlaVA
----------------------------
A pirate walks into a bar with a huge ship's steering wheel down his pants.
The bartender asks, "Are you aware that you have a steering wheel down your pants?"
The pirate replies, "Arrrrr! It's driving me nuts!
 
or my favorite (found not coded by me)

just call string1.trim()
Code:
<script language="JavaScript" type="text/javascript">
String.prototype.trim = function() {
/*This file retrieved from the JS-Examples archives
[URL unfurl="true"]http://www.js-x.com[/URL]
1000s of free ready to use scripts, tutorials, forums.
Author: Brock Weaver - 0*/

 // skip leading and trailing whitespace
 // and return everything in between
  var x=this;
  x=x.replace(/^\s*(.*)/, "$1");
  x=x.replace(/(.*?)\s*$/, "$1");
  return x;
}
</script>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top