?
Solved

How to trim a string

Posted on 2002-05-02
6
Medium Priority
?
200 Views
Last Modified: 2012-06-27
Hi, all

Could you please tell me how to trim a string using javascript? Thanks
0
Comment
Question by:CodeLover
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 32

Expert Comment

by:Batalf
ID: 6987753
This is some code, I found posted by mplungjan earlier:

<script language="JavaScript">
<!--
// Add Trim function to ALL strings
String.prototype.trim = function(){
  return( (ar=/^s*([sS]*S+)s*$/.exec(this)) ? ar[1] : "" );
}

// to test:

var str;

str = " Hello World   ";

str = str.trim();
alert('>'+str+'<');
//-->
</script>
0
 
LVL 30

Expert Comment

by:third
ID: 6987795
other example

LTRIM, RTRIM, TRIM functions.

http://www.vermontsoftware.com/Javascript/trim.html
0
 
LVL 2

Expert Comment

by:svd2002
ID: 6987927
function trim_string()
     {
         var ichar, icount;
         var strValue = this;
         
         ichar = strValue.length - 1;
         icount = -1;
         while (strValue.charAt(ichar)==' ' && ichar > icount)
             --ichar;
         if (ichar!=(strValue.length-1))
             strValue = strValue.slice(0,ichar+1);
         ichar = 0;
         icount = strValue.length - 1;
         while (strValue.charAt(ichar)==' ' && ichar < icount)
             ++ichar;
         if (ichar!=0)
             strValue = strValue.slice(ichar,strValue.length);
         return strValue;
     }
0
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 
LVL 1

Expert Comment

by:rajeev1972
ID: 6987979
Use This :

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<SCRIPT LANGUAGE="JavaScript">
<!--
function trimSpace(passedVal)
{
     var i, j;

     for(i=0; i<passedVal.length; i++ )
     {
          if (passedVal.charAt(i)!=" ")
               break;
     }

     for(j=passedVal.length-1; j>=0; j-- )
     {
          if (passedVal.charAt(j)!=" ")
               break;
     }

     if (i > j)
          passedVal = "";
     else
          passedVal = passedVal.substring(i, j+1);

     return(passedVal);
}
//-->
</SCRIPT>
</HEAD>

<BODY onLoad = "alert('>' + trimSpace('   Rajeev Kumar   ') + '<')">

</BODY>
</HTML>
0
 
LVL 2

Expert Comment

by:svd2002
ID: 6988172
With reference to the comment i have given above,
just add the following line after the completion of a function:

String.prototype.Trim = trim_string;

And then you can extend the string object to include a trim function like:

strTemp = strTemp.Trim();

0
 
LVL 33

Accepted Solution

by:
knightEknight earned 200 total points
ID: 6988425
// This method trims white space off both ends of this string and returns the result.
String.prototype.trim = function()
{
   return( this.replace(/^\s*([\s\S]*\S+)\s*$|^\s*$/,'$1') );
}

  // example:
  var str = "  hello  world  ";
  str = str.trim();
  alert(str);
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
In this blog, we’ll look at how improvements to Percona XtraDB Cluster improved IST performance.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question