JavaScript: The Definitive Guide

24.161. String: support for strings

ECMAScript v1: Object String

24.161.1. Constructor

new String(s) // Constructor function String(s) // Conversion function

24.161.1.1. Arguments

s

The value to be stored in a String object or converted to a primitive string.

24.161.1.2. Returns

When 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

length

The number of characters in the string.

24.161.3. Methods

charAt( )

Extracts the character at a given position from a string.

charCodeAt( )

Returns the encoding of the character at a given position in a string.

concat( )

Concatenates one or more values to a string.

indexOf( )

Searches the string for a character or substring.

lastIndexOf( )

Searches the string backward for a character or substring.

localeCompare( )

Compares strings using locale-specific ordering.

match( )

Performs pattern matching with a regular expression.

replace( )

Performs a search-and-replace operation with a regular expression.

search( )

Searches a string for a substring that matches a regular expression.

slice( )

Returns a slice or substring of a string.

split( )

Splits a string into an array of strings, breaking at a specified delimiter string or regular expression.

substr( )

Extracts a substring of a string; a variant of substring( ).

substring( )

Extracts a substring of a string.

toLowerCase( )

Returns a copy of the string, with all characters converted to lowercase.

toString( )

Returns the primitive string value.

toUpperCase( )

Returns a copy of the string, with all characters converted to uppercase.

valueOf( )

Returns the primitive string value.

24.161.4. Static Methods

String.fromCharCode( )

Creates a new string using the character codes passed as arguments.

24.161.5. HTML Methods

Since 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:

anchor( name)

Returns a copy of the string, in an <a name=> environment.

big( )

Returns a copy of the string, in a <big> environment.

blink( )

Returns a copy of the string, in a <blink> environment.

bold( )

Returns a copy of the string, in a <b> environment.

fixed( )

Returns a copy of the string, in a <tt> environment.

fontcolor( color)

Returns a copy of the string, in a <font color=> environment.

fontsize( size)

Returns a copy of the string, in a <font size=> environment.

italics( )

Returns a copy of the string, in an <i> environment.

link( url)

Returns a copy of the string, in an <a href=> environment.

small( )

Returns a copy of the string, in a <small> environment.

strike( )

Returns a copy of the string, in a <strike> environment.

sub( )

Returns a copy of the string, in a <sub>

sup( )

Returns a copy of the string, in a <sup> environment.

24.161.6. Description

Strings 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 Also

Chapter 3

Категории