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
Solved

How to trim a string

Posted on 2002-05-02
6
196 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
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

In my daily work (mainly using ASP.net), I need to write a lot of JavaScript code. One of the most repetitive tasks I do are the jQuery Ajax calls. You know: (CODE) I don't know if for you it's the same, but for me is soooo tedious to write the …
In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
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…

829 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