Wrox Programmer Forums
|
Javascript General Javascript discussions.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Javascript section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old June 20th, 2006, 08:23 AM
Authorized User
 
Join Date: Jun 2006
Posts: 29
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to ajit
Default How to trim strings

In Javascript what is the function for trimming the string. trim() did not work.

Ajit
__________________
Ajit
 
Old June 20th, 2006, 08:37 AM
Friend of Wrox
 
Join Date: Oct 2004
Posts: 553
Thanks: 0
Thanked 1 Time in 1 Post
Send a message via MSN to vinod_yadav1919 Send a message via Yahoo to vinod_yadav1919
Default

Hi Ajit!!
function ltrim(str)
{
return str.replace(/^[ ]+/, '');
}

function rtrim(str)
{
return str.replace(/[ ]+$/, '');
}

function trim(str)
{
return ltrim(rtrim(str));
}

hope this will help you



Cheers :)

vinod
 
Old June 20th, 2006, 09:27 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

If you add these three lines of script before you need the functions then you can use lTrim, rTrim and trim functions:
Code:
String.prototype.lTrim = function(){return this.replace(/^\s*/g,"");}
String.prototype.rTrim = function(){return this.replace(/\s*$/g,"");}
String.prototype.trim = function(){return this.lTrim().rTrim();}
Examples:
Code:
var s = "    Using trim functions    ";
alert("lTrim: !" + s.lTrim() + "!");
alert("rTrim: !" + s.rTrim() + "!");
alert("trim: !" + s.trim() + "!");
--

Joe (Microsoft MVP - XML)
 
Old June 20th, 2006, 10:05 AM
Authorized User
 
Join Date: Jun 2006
Posts: 29
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to ajit
Default

Hi Joe, it worked. But i could'nt understand the concept. These spacial characters are used without double codes. They mean something. If U have time, can U plz tel me abt these the way these spacial characters are used in javascript. Here they are used to ignore the spaces(trim())

Ajit
 
Old June 20th, 2006, 10:30 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

You need to search for regular expressions.

--

Joe (Microsoft MVP - XML)





Similar Threads
Thread Thread Starter Forum Replies Last Post
trim with ListFolderContents melkin Classic ASP Components 1 September 12th, 2008 03:03 PM
XSLT TRIM Function sagrock XSLT 1 November 23rd, 2007 08:39 AM
Trim MathLearner VB Databases Basics 2 May 16th, 2007 11:38 PM
String.trim() mrfox BOOK: Professional JavaScript for Web Developers ISBN: 978-0-7645-7908-0 1 January 8th, 2006 12:23 AM
Trim() NeoGeo ASP.NET 1.0 and 1.1 Basics 2 January 21st, 2004 11:19 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.