DekGenius.com
Team LiB   Previous Section   Next Section
String support for strings

Availability

JavaScript 1.0; JScript 1.0; ECMAScript v1

Inherits from/Overrides

Inherits from Object

Constructor

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

Arguments

s

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

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.

Properties

length

The number of characters in the string.

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.

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.

substring( )

Extracts a substring of a string.

substr( )

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

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.

Static Methods

String.fromCharCode( )

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

HTML Methods

Since JavaScript 1.0 and JScript 1.0, 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-side 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 a <i> environment.

link( url)

Returns a copy of the string, in a <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> environment.

sup( )

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

Description

Strings are a primitive data type 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: there are methods for extracting a character or a substring from the string or searching for a character or a substring, for example. 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 like String.toUpperCase( ) return an entirely new string, without modifying the original.

In Netscape implementations of JavaScript 1.2 and later, strings behave like read-only arrays of characters. For example, to extract the 3rd character from a string s, you could 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 of Netscape's implementations is not standard, you should usually avoid using it.

See Also

Chapter 3

    Team LiB   Previous Section   Next Section