Solved

How to trim a string

Posted on 2002-05-02
6
198 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
The Ultimate Checklist to Optimize Your Website

Websites are getting bigger and complicated by the day. Video, images, custom fonts are all great for showcasing your product/service. But the price to pay in terms of reduced page load times and ultimately, decreased sales, can lead to some difficult decisions about what to cut.

 
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

The Ultimate Checklist to Optimize Your Website

Websites are getting bigger and complicated by the day. Video, images, custom fonts are all great for showcasing your product/service. But the price to pay in terms of reduced page load times and ultimately, decreased sales, can lead to some difficult decisions about what to cut.

Question has a verified solution.

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

This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
This article discusses how to create an extensible mechanism for linked drop downs.
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…

717 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