Welcome to Bucaro TecHelp!

Bucaro TecHelp
Maintain Your Computer and Use it More Effectively
to Design a Web Site and Make Money on the Web

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

Victims of Sandy Hook

Stop the Slaughter of Innocents. Congress is bought and paid for by gun lunatics and gun promotion groups. If you want to live in a safe America, help buy Congress back for America. Send a donation to Mayors Against Illegal Guns, 909 Third Avenue, 15th Floor New York, NY 10022

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.

RSS Feed RSS Feed

Follow Stephen Bucaro Follow @Stephen Bucaro



Web Design Sections

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