Solved

How to trim a string

Posted on 2002-05-02
6
192 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
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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 50 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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

When you need to keep track of a simple list of numbers or strings, the Array object is your most direct tool.  As we saw in my earlier EE Article (http://www.experts-exchange.com/A_3488.html), typical array handling might look like this: (CODE) B…
In this article, we'll look how to sort an Array in JavaScript, including the more advanced techniques of sorting a collection of records either ascending or descending on two or more fields. Basic Sorting of Arrays First, let's look at the …
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…

760 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now