JavaScript Strings

Posts about manipulating strings using JavaScript

Pad A String In JavaScript

24th September 2008

Use the following function to pad a string with to a set length with a given string. The function takes three parameters. The string to be padded, the total number of characters that the string must be, and the string to be added. If the third parameter is not given then 0 is used as a default.

JavaScript Word Counter

23rd September 2008

Counting the number of words in a block of text can be tricky. Users tend to create sections of white space that can throw out most scripts that don't take this into consideration.

I have created a tool for #! code that looks at a textarea of a form, and prints the number of words into an input box in the same form. The textarea calls a function called textCounter() every time a key is pressed. Here is the HTML of the form.

Flashing JavaScript Text

27th January 2008

Here is a simple function that makes text in a tag fade into one colour slowly before quickly fading back into the original colour. If the background is the same colour as the text then the text will appear to fade in and out.

JavaScript Working Text

18th January 2008

Letting the user now that something in the background is working is an essential part of website usability. If nothing at all happens then the user will more than likely either try again or go elsewhere. A good way of doing this is to have a little bit of text that says "Working" and animate dots behind it. Here is a function that will do this.

Some Useful String Functions In JavaScript

27th December 2007

Here are a few of the built in JavaScript functions available.

To get the length of a string use the length variable. This returns the number of characters in a string.

  1. var str = 'abcdef';
  2. str.length; // returns 6

The charAt function will return the character at the point specified in the parameter. So to get the first character of a string use: