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.
Welcome to Bucaro TecHelp!

Welcome to 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 Advertise on Bucaro TecHelp Advertise Here RSS News Feeds News Feeds

HTML5 Solutions: Essential Techniques for HTML5 Developers

Essential Techniques for HTML5 Developers

HTML5 brings the biggest changes to HTML in years. Web designers now have new techniques, from displaying video and audio natively in HTML, to creating realtime graphics on a web page without a plugin.

This book provides a collection of solutions to all of the most common HTML5 problems. Every solution contains sample code that is production-ready and can be applied to any project.

Click Here

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



Web Design Sections

Eloquent JavaScript: A Modern Introduction to Programming

Eloquent JavaScript

Eloquent JavaScript Eloquent JavaScript goes beyond the cut-and-paste scripts of the recipe books and teaches you to write code that's elegant and effective. You'll start with the basics of programming, and learn to use variables, control structures, functions, and data structures. Then you'll dive into the real JavaScript artistry: higher-order functions, closures, and object-oriented programming.

Reader Anthony says, "This book is not your typical Javascript book. Others have a utilitarian approach. In stark contrast, Eloquent JavaScript does not merely provide you a checklist of things to learn but rather paints a panorama of the possibilities that programming provides. Javascript is merely the tool used to introduce these to the reader.

Click here for more information.


[Site User Agreement] [Advertise on This site] [Search This Site] [Contact Form]
Copyright©2001-2011 Bucaro TecHelp 13771 N Fountain Hills Blvd Suite 114-248 Fountain Hills, AZ 85268