Chapter 16
Handling Strings
String Indexing
Strings are constructed from a variable number of characters. They are very similar to
arrays which are constructed of elements as well. The elements of an array are placed in a
certain order, where each element has its index, or subscript. The index determines the
position of the element in the array. Strings are characters chained in a certain order as
well. Each character has its position, or index. The indexing of strings, like that of
arrays, is zero-based. The index of the first character of a string is 0, the index of the
second one is 1, that of the third one is 2, and so on.
Characters
JavaScript, unlike most languages, does not include an explicit character (char)
data type. A character is simply a string constructed of only one character. Characters of
a string must be visual. That is, each symbol that appears in a string is a character.
However, not all characters that appear in the ASCII table can be characters of a string,
because some are not visual. An escape sequence is a sequence of characters that
represents a single special character, which is usually difficult to enter via the
keyboard. For example, take a look at the following string:
"car\'s wheel"
It appears as if the string contains 12 characters (c, a, r, \, ', s, , w, h, e, e, l).
However, when you print the string you can see only 11 characters (c, a, r, ', s, , w, h,
e, e, l). Therefore, the string consists of 11 characters. The fourth character is an
apostrophe, not a backslash, because the sequence “\'” is shorthand for an apostrophe.
Creating Strings
String is a very special built-in object, because of the way you can create
it. All strings belong to the built-in object String, so you can create strings
as instances of the String object:
var str = new String("Hello!")
The general syntax is:
stringObjectName = new String(string
stringObjectName is the name of the string object you are creating. string
is any string, including strings that were not created via the String
constructor. Strings created via this constructor function are considered objects. If you
test their data type using the typeof operator you will find that they are
objects, not strings. However, all the string properties and methods work on such objects.
Another way to create a string is by simply quoting a sequence of characters. If you
want to use it as an object, you can follow the identifier by the desired property or
method.
Netscape first implemented strings as objects (created via the constructor) in Netscape
Navigator 3.0. Microsoft included this feature in version 3.0 of its browser as well.
Although it is rarely used, creating strings with the constructor is sometimes essential.
Netscape implemented this feature due to a bug that disabled passing scripts among the
browser’s window and frames. In Navigator 2.0, you had to concatenate an empty string as
a workaround for the bug. Generally speaking, you should create a string via the String
constructor whenever your script involves windows or frames. These topics are covered
later in the book.
String Length
The String object combines many methods and one property, length.
This property reflects the length of its calling object. Here are a few examples:
var str1 = "abc"
var str2 = "a b c"
var str3 = "\"abc\""
document.write(str1.length + "<BR>")
document.write(str2.length + "<BR>")
document.write(str3.length)
The output of this script is:
3
5
5
The index of a string’s last character is equal to its length (number of characters)
minus one. The string’s length is the index of the non-existing character following the
string’s last character.
HTML Formatting Methods
You have seen a great amount of HTML formatting throughout the book. You can print HTML
tags by combining strings to receive the desired tag and attribute values. Here is an
example:
var openBold = "<B>"
var closeBold = "</B>"
var message = "Something"
document.write("<B>Something</B>")
The result of this script is that the string “Something” is printed in
bold. JavaScript provides methods to simplify this process. For example, you can create
the exact same output using the following statement:
document.write("Something".bold())
The following table lists all these methods along with the HTML they generate:
Table 16-1. HTML formatting methods.
|
Method Name |
Example |
Returned Value |
|
anchor |
"text".anchor("anchorName") |
<A NAME="anchorName"> text</A> |
big |
"text".big() |
<BIG>text</BIG> |
blink |
"text".blink() |
<BLINK>text</BLINK> |
bold |
"text".bold() |
<BOLD>text</BOLD> |
fixed |
"text".fixed() |
<TT>text</TT> |
fontcolor |
"text".fontcolor("red") |
<FONT COLOR="red"> text</FONT> |
fontsize |
"text".fontsize(–1) |
<FONT SIZE=–1>text</FONT> |
italics |
"text".italics() |
<I>text</I> |
link |
"text".link("URL") |
<A HREF="URL">text</A> |
small |
"text".small() |
<SMALL>text</SMALL> |
strike |
"text".strike() |
<STRIKE>text</STRIKE> |
sub |
"text".sub() |
<SUB>text</SUB> |
sup |
"text".sup() |
<SUP>text</SUP> |
toLowerCase |
"TexT".toLowerCase() |
text |
toUpperCase |
"TexT".toUpperCase() |
TEXT |
|
The following screen shows the rendering of the word text using each of the
above methods:
Figure
16-1. The String methods return formatted
HTML.
You can “chain” methods together in order to apply more than one formatting
conversion. For example, if you want an italic bold uppercase string, you can use the
following expression: toUpperCase().bold().italics(). The evaluation here is done
from left to right. The following list outlines the stages of the evaluation, where the
calling string is the literal "text":
"text".toUpperCase().bold().italics()
"TEXT".bold().italics()
"<B>TEXT</B>".italics()
"<I><B>TEXT</B></I>"