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 BTH]  [User Agreement]  [Privacy Policy]  [Site Map]  [Contact Form]  [Advertise on BTH]  [News Feed]

Google
Web
This Site

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.

Web Design Sections

RSS Feed RSS Feed

Easy Java Script Code
Easy Slide Show Code
Easy Slide Show Code with Mouseover Pause
Easy Slide Show Code With Linked Slides
Slide Show with Different Size Images
Easy Java Script Timer Code
Easy Java Script Windows
Easy Rollovers
Easy Picture Transition Effects
Easy Moving Popup Code
Easy Picture Zoom Code
Easy Picture Panning Code
Easy Java Script Animation
Easy JavaScript Picture Selector Code
Easy Animated Banners with Java Script
Easy Java Script Form Validation
Easy Drag n Drop Code
Easy Graph Generating Code
Easy Code to Sort a Table by Column
Easy Expanding Menu Code
Web Designer's Reference
Object-Oriented JavaScript
Easy Fading Text Banner Code
Easy Expanding Banner Code
Calendars for Your Website
Date Picker For Your Web Site
Calculators For Your Web Site
Loan Payment Calculator
Length Units Converter
Body Mass Index
Fahrenheit to Celsius Converter
Decimal to Hexidecimal Converter
Easy Code for Screen Tape Adding Machine
Code for Java Script Circle⁄Sphere Calculator
Code for Java Script Cube⁄Box Volume Calculator
Round a Float to 4 Digits to the Right of the Decimal Point
Disable IE8 Accelerators on Your Website
Prevent Automated Form Submissions With Java Script
Make Your Own Graphical Digital Clock
Let Your Web site Visitors Set Font Size
How to Disable the Browser Back Button
Add More Bang to Your Content With Keyword Popup Menus
Put a Music Off Switch on Your Webpage
Java Script Random Password Generator
Password Protection Using the JavaScript Cookie Method
Password Protection Using the Frames Method
Replace Drop-down with Drag-and-drop
Submit Forms Without CGI
Code for a Less annoying Popup Window
Java Script Comparison Operators
Using the Java Script Array Object
Using the Java Script Date Object
Java Script Message Boxes
Java Script Trim Function
Convert Mixed Number to Decimal
How to Shuffle the Deck With Java Script
Web Site Menus : Which Section Am I In?
How Far Did the User Scroll?
Where Did the User Click?
Four Ways to Use Java Script Event Handlers
Create Your Own Database Using Only Notepad : CDV


Your Career Quiz
[Site User Agreement]  [Advertise on This site]  [Search This Site]  [Contact Form]
Copyright©2001-2009 Bucaro TecHelp P.O.Box 18952 Fountain Hills, AZ 85269