Welcome to Bucaro TecHelp!

Bucaro TecHelp
HTTPS Encryption not required because no account numbers or
personal information is ever requested or accepted by this site

About Bucaro TecHelp About BTH User Agreement User Agreement Privacy Policy Privacy Site Map Site Map Contact Bucaro TecHelp Contact RSS News Feeds News Feeds

Java Script Trim Function

A "trim function" is a bit of code that removes leading and trailing blanks from a character string. You might use a trim function to clean up the data from a user input form before performing further processing on it.

Google the phrase "Java Script Trim" and you'll find that everybody and their grandmother has a Java Script Trim function to offer. Some of these coders think the fewer the lines used, the faster the code will work. Some enjoy being in a continuous state of puzzlement and so they create code like this shown below.

function trim(s)
{
   return s.replace(/^\s*(.*?)\s*$/,"$1");
}

This code uses a regular expression, and although regular expressions are very powerful and useful when used in the proper application, a trim function is not one of them. There are two problems with using a regular expression; a look at the resulting machine code would show that it's about the slowest way to perform this simple function, and since regular expressions are rarely required in general coding, whenever I see them I have to review.

In any case, I offer the following code for a Java Script Trim function. My goal for this code is not to set a speed record or demonstrate my programming prowess, but instead to provide something that is easy to understand so that you might feel confident in using it in your own projects.

// trim leading and trailing spaces
function trim(s)
{
  var myString = s;
  var j = 0;

  // trim front of string
  for(var i = 0; i < myString.length; i++)
  {
    if(myString.charAt(i) != " ")
    {
      break
    }
    else
    {
      j += 1;
    }
  }
  myString = myString.substring(j, myString.length);

  // trim back of string
  j = myString.length;
  for(var i = myString.length - 1; i >= 0; i--)
  {
    if(myString.charAt(i) != " ")
    {
      break
    }
    else
    {
      j -= 1;
    }
  }
  myString = myString.substring(0, j);

  return myString;
}

The above code simply and efficiently removes leading and trailing blanks from a character string. Now if you regular expression lovers are such hot shots, send me a template that (removes not replaces) blanks within a character string.

More Java Script Code:
• Code for a Less annoying Popup Window
• Basic State Machines
• How to Disable the Browser Back Button
• JavaScript Code for Binary to Hexadecimal - Hexadecimal to Binary Conversion
• Java Script Code to Re-arrange Items in a Select List
• Password Protection Using the JavaScript Cookie Method
• Java Script Code for Directory File List
• Easy Slide Show Code With Linked Slides
• Round a Float to 4 Digits to the Right of the Decimal Point
• Easy Java Script Code to Fill Array With Random Numbers

RSS Feed RSS Feed

Follow Stephen Bucaro Follow @Stephen Bucaro



Fire HD
[Site User Agreement] [Privacy Policy] [Site map] [Search This Site] [Contact Form]
Copyright©2001-2024 Bucaro TecHelp 13771 N Fountain Hills Blvd Suite 114-248 Fountain Hills, AZ 85268