IT Community - Software Programming, Web Development and Technical Support

Trimming strings in JavaScript

This is a discussion on Trimming strings in JavaScript within the HTML, CSS and Javascript Coding Techniques forums, part of the Web Development category; Hi all.... Trimming strings in JavaScript Thanks & Regards Pvinoth....


Go Back   IT Community - Software Programming, Web Development and Technical Support > Web Development > HTML, CSS and Javascript Coding Techniques

Register FAQ Members List Calendar Mark Forums Read
  #1 (permalink)  
Old 09-13-2007, 05:11 AM
Pvinothkumar Pvinothkumar is offline
D-Web Analyst
 
Join Date: Sep 2007
Posts: 237
Pvinothkumar is on a distinguished road
Default Trimming strings in JavaScript

Hi all....

Trimming strings in JavaScript

Thanks & Regards
Pvinoth.
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 09-13-2007, 06:21 AM
a.deeban a.deeban is offline
D-Web Analyst
 
Join Date: May 2007
Posts: 279
a.deeban is on a distinguished road
Default Re: Trimming strings in JavaScript

Hi...


If you've worked with other C-based languages, like C, C++ or C#, you'll know that there's a very handy function called trim() which is able to automatically remove leading and trailing spaces from the string. Unfortunately, this function has been left out of JavaScript. However, you can easily make your own string trimming function, as shown below:

function strtrim(string)
{
//Remove leading spaces
while(string.charAt(0) == " ")
string = string.substring(1, string.length);

//Remove trailing spaces
while(string.charAt(string.length-1) == " ")
string = string.substring(string, string.length-1);

return string;
}

Then, to trim a string, simply use:

myString = strtrim(myString);

A better way to implement this function is to acutally add it to the string object, so the syntax is myString.trim() rather than strtrim(myString):

String.prototype.trim = function()
{
var string = this;

//Remove leading spaces
while(string.charAt(0) == " ")
string = string.substring(1, string.length);

//Remove trailing spaces
while(string.charAt(string.length-1) == " ")
string = string.substring(string, string.length-1);

return string;
}


thnx....
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 09-18-2007, 10:34 PM
kingmaker kingmaker is offline
D-Web Genius
 
Join Date: Jun 2007
Posts: 882
kingmaker is on a distinguished road
Send a message via Yahoo to kingmaker
Default Re: Trimming strings in JavaScript

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;
}
__________________
The KINGMAKER
Makes Every Thing Possible

Stuffs (My Blog)
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to concatenate strings with Perl? amansundar Perl 3 11-28-2007 09:39 PM
How does JavaScript Reporter differ from JavaScript editors? Pvinothkumar HTML, CSS and Javascript Coding Techniques 1 09-13-2007 05:59 AM
Connection Strings Info Gopisoft VB.NET Programming 0 07-17-2007 05:42 AM


All times are GMT -7. The time now is 11:20 AM.


Copyright ©2004 - 2007, DiscussWeb. All Rights Reserved.

SEO by vBSEO 3.0.0