Previous page

(Stringing you along)!


What you already know!

You already know a lot about strings. Here we flesh things out a teeny bit.

Making strings and converting them

You already know how to create a string. Javascript has weak typing and tendency to coerce anything it doesn't understand into a string (if it has an excuse - we've already seen how

3 + "5"

gives us "35" and not eight as we might expect). Make a variable either local or global, put a string value into it, and voila - a string! You've also learnt the trick required to turn a number into a string -

123 + ""

gives us the string "123". Conversely, we know that eval and the related parseInt and parsefloat can turn strings into numbers (or errors)!

Methods you have used

Similarly you have already used several string methods:
  • indexOf() searches for a substring within the string, returning the offset of the first character that completely matches the substring. Remember that in JavaScript we start counting at zero. Take note - you can give a second, numeric, argument to indexOf and it then starts its search at this numeric position.

  • substring() returns a substring. The first argument passed to substring specifies the first character to copy, the second (optional) argument says where to stop. Note that (a) again we start counting from zero, (b) if we specify a second argument then this last character specified is not included in the substring, and that (c) if we leave out the second argument, the substring is the rest of the string from the character specified by the first argument onwards. The indexOf and substring methods are demonstrated in the JavaScript-generated code at the start of this document (View the source to see I didn't cheat)!

  • toLowerCase() makes a copy of the string, but in lower case.

  • toUpperCase is similar, but returns an UPPER CASE string.

There are several other methods available for strings, but you'll find most of them to be distressingly unhelpful. We detail them in the next section.

String methods

Here the remaining methods are:

Method What it does
charAt() Returns a string the same as the character at the index supplied as the single argument of this method. A similar result can be obtained using substring, so this method adds little value.
lastIndexOf() similar to indexOf, but searches through the string and finds the rightmost (i.e. last) substring that matches the string you're searching for. Returns a numeric offset, just like indexOf(), with -1 signifying "not found".
split() This method is potentially very useful - you provide a "separator" string, and it uses this separator to divide the string into an array of substrings.
fromCharCode() This is a useful addition (in JavaScript 1.2) - it converts a number to a string character using the corresponding ASCII code. For example, 65 decimal becomes "A". Note that on the Mac, IE 4 screws up some of the conversions.
And that's about it for strings. The whole host of remaining methods simply write the string to the page, just like document.write() ! All they do is dress up the string in the relevant HTML tags, which you can easily do with document.write(). For completeness they are:

anchor(anchorname), big(), bold(), fixed(), fontcolor(color), fontsize(size), italics(), link(href), small(), strike(), sub(), sup(). Why, there's even a blink().

What's missing?

Think about it - do you know how to convert a single string character (say "a") to the internal representation that is used in your computer? If you've played with computers a bit, you'll know that in the past, dozens of different methods have been used to store characters as numbers. There are now two standards - ASCII (the American Standard Code for Information Interchange) and more recently, UNICODE. ASCII is simple, only attaching character values to the first 128 numbers you can store in a byte (characters 0 to 127, or 00 to 7F hex, if you prefer). UNICODE is enormous, but defines glyphs for literally thousands of different numbers - it uses two bytes to store each character, so the maximum number of UNICODE characters is around 65000, all of which have not been used up yet. Fortunately UNICODE includes ASCII as its first 128 characters.

Only with JavaScript 1.2 do we have a method for converting a character to its Unicode equivalent - this is .charCodeAt(offset) where offset is an integer offset of the character in question. This complements fromCharCode, discussed above.


We haven't learnt much that we don't know already. We'd best return!

Webpage author Last update: 2000-10-7