Solved

How to trim a string

Posted on 2002-05-02
6
195 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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
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…

777 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