CD-91Chapter 10 .Strings, Math, and Dates var msg (Web site optimization)
Wednesday, August 22nd, 2007CD-91Chapter 10 .Strings, Math, and Dates var msg = Four score msg += and seven msg += years ago, You can also combine the operators if the need arises: var msg = Four score msg += and seven + years ago I use the add-by-value operator a lot when accumulating HTML text to be written to the current document or another window. String methods Of all the core JavaScript objects, the Stringobject has the most diverse collection of methods associated with it. Many methods are designed to help scripts extract segments of a string. Another group, rarely used in my experience, wraps a string with one of several style-oriented tags (a scripted equivalent of tags for font size, style, and the like). To use a string method, the string being acted upon becomes part of the reference followed by the method name. All methods return a value of some kind. Most of the time, the returned value is a converted version of the string object referred to in the method call but the original string is still intact. To capture the modified version, you need to assign the results of the method to a variable: var result = string.methodName() The following sections introduce you to several important string methods available to all browser brands and versions. Changing string case Two methods convert a string to all uppercase or lowercase letters: var result = string.toUpperCase() var result = string.toLowerCase() Not surprisingly, you must observe the case of each letter of the method names if you want them to work. These methods come in handy when your scripts need to compare strings that may not have the same case (for example, a string in a lookup table compared with a string typed by a user). Because the methods don t change the original strings attached to the expressions, you can simply compare the evaluated results of the methods: var foundMatch = false if (stringA.toUpperCase() == stringB.toUpperCase()) { foundMatch = true } String searches You can use the string.indexOf() method to determine if one string is contained by another. Even within JavaScript s own object data, this can be useful information. For example, another property of the navigatorobject in Chapter 3 (navigator.userAgent) reveals a lot about the browser that loads the page. A script can investigate the value of that property for the existence of, say, Win to determine that the user has a Windows operating system. That short string might
You need excellent and relaible webhost company to host your web applications? Then pay a visit to Inexpensive Web Hosting services.