Sams Teach Yourself JavaScript in 24 Hours

Sams Teach Yourself JavaScript in 24 Hours

By Michael Moncur

Using String Objects

You've already used several strings during the first few hours of this book. Strings store a group of text characters, and are named similarly to other variables. As a simple example, this statement assigns the string This is a test to a string variable called test:

test = "This is a test";

Creating a String Object

JavaScript stores strings as String objects. You usually don't need to worry about this, but it will explain some of the techniques for working with strings, which use methods (built-in functions) of the String object.

There are two ways to create a new String object. The first is the one you've already used, while the second uses official object syntax. The following two statements create the same string:

test = "This is a test";
test = new String("This is a test");

The second statement uses the new keyword, which you use to create objects. This tells the browser to create a new String object containing the text This is a test, and assigns it to the variable test.

Assigning a Value

You can assign a value to a string in the same way as any other variable. Both of the examples in the previous section assigned an initial value to the string. You can also assign a value after the string has already been created. For example, the following statement replaces the contents of the test variable with a new string:

test = "This is only a test.";

You can also use the concatenation operator (+) to combine the values of two strings. Listing 5.1 shows a simple example of assigning and combining the values of strings.

Example 5.1. Assigning values to strings and combining them

<html>
<head>
<title>String Test</title>
</head>
<body>
<h1>String Test</h1>
<script LANGUAGE="JavaScript" type="text/javascript">;
test1 = "This is a test. ";
test2 = "This is only a test.";
both = test1 + test2;
alert(both);
</script>
</body>
</html>

This script assigns values to two string variables, test1 and test2, and then displays an alert with their combined value. If you load this HTML document in a browser, your output should resemble Figure 5.1.

05fig01.jpg

Figure 5.1 The output of the string example script.

In addition to using the + operator to concatenate two strings, you can use the += operator to add text to a string. For example, this statement adds a period to the current contents of the string sentence:

sentence += ".";

Calculating the String's Length

From time to time, you may find it useful to know how many characters a string variable contains. You can do this with the length property of String objects, which you can use with any string. To use this property, type the string's name followed by .length.

For example, test.length refers to the length of the test string. Here is an example of this property:

test = "This is a test.";
document.write(test.length);

The first statement assigns the string This is a test to the test variable. The second statement displays the length of the string—in this case, 15 characters.

Converting the String's Case

Two methods of the String object allow you to convert the contents of a string to all uppercase or all lowercase:

For example, the following statement displays the value of the test string variable in lowercase:

document.write(test.toLowerCase());

Assuming that this variable contained the text This Is A Test, the result would be the following string:

this is a test

Note that the statement doesn't change the value of the text variable. These methods return the upper- or lowercase version of the string, but they don't change the string itself. If you want to change the string's value, you can use a statement like this:

test = test.toLowerCase();

Share ThisShare This

Informit Network