24.161. String: support for stringsECMAScript v1: Object String24.161.1. Constructornew String(s) // Constructor function String(s) // Conversion function 24.161.1.1. Arguments
24.161.1.2. ReturnsWhen String( ) is used as a constructor with the new operator, it returns a String object, which holds the string s or the string representation of s. When the String( ) constructor is used without the new operator, it simply converts s to a primitive string and returns the converted value. 24.161.2. Properties
24.161.3. Methods
24.161.4. Static Methods
24.161.5. HTML MethodsSince the earliest days of JavaScript, the String class has defined a number of methods that return a string modified by placing it within HTML tags. These methods have never been standardized by ECMAScript but can be useful in both client- and server-side JavaScript code that dynamically generates HTML. If you are willing to use nonstandard methods, you might create the HTML source for a bold, red hyperlink with code like this: var s = "click here!"; var html = s.bold( ).link("javascript:alert('hello')").fontcolor("red"); Because these methods are not standardized, they do not have individual reference entries in the pages that follow:
24.161.6. DescriptionStrings are a primitive datatype in JavaScript. The String class type exists to provide methods for operating on primitive string values. The length property of a String object specifies the number of characters in the string. The String class defines a number of methods for operating on strings; for example, there are methods for extracting a character or a substring from the string or searching for a character or a substring. Note that JavaScript strings are immutable: none of the methods defined by the String class allows you to change the contents of a string. Instead, methods such as String.toUpperCase( ) return an entirely new string, without modifying the original. In implementations of JavaScript derived from the original Netscape code base (such as the implementation in Firefox), strings behave like read-only arrays of characters. For example, to extract the third character from a string s, you can write s[2] instead of the more standard s.charAt(2). In addition, when the for/in statement is applied to a string, it enumerates these array indexes for each character in the string. (Note, however, that the length property is not enumerated, as per the ECMAScript specification.) Because this string-as-array behavior is not standard, you should avoid using it. 24.161.7. See AlsoChapter 3 |