|
Free Open Book
Sams Teach Yourself JavaScript in 24 Hours |
Using String ObjectsYou'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 ObjectJavaScript 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, whereas the second uses object-oriented 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. By the Way Although you can create a string using object-oriented syntax, the standard JavaScript syntax is simpler, and there is no difference in the strings created by these two methods. Assigning a ValueYou 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. Listing 5.1. Assigning Values to Strings and Combining Them
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. 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 += "."; By the Way The plus sign (+) is also used to add numbers in JavaScript. The browser knows whether to use addition or concatenation based on the types of data you use with the plus sign. If you use it between a number and a string, the number is converted to a string and concatenated. Calculating the String's LengthFrom time to time, you might 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 stringin this case, 15 characters. The length property is a read-only property, so you cannot assign a value to it to change a string's length. By the Way Remember that although test refers to a string variable, the value of test.length is a number and can be used in any numeric expression. Converting the String's CaseTwo methods of the String object enable 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(); By the Way Note that the syntax for these methods is similar to the length property introduced earlier. The difference is that methods always use parentheses, whereas properties don't. The toUpperCase and toLowerCase methods do not take any parameters, but you still need to use the parentheses. |
Main Menu |
| 500 Juegos Gratis | 500 Giochi Gratis | 500 Jeux Gratuits | 500 Jogos Gratis | 500 Kostenlose Spiele |