JavaScript: The Definitive Guide

Previous Chapter 21
JavaScript Reference
Next
 

String Object

Name

String Object---support for strings

Availability

Navigator 2.0, Internet Explorer 3.0; enhanced in Navigator 3.0

Constructor

new String(value) Navigator 3.0 only

Arguments

value

The initial value of the String object being created. This argument will be converted to a string, if necessary.

Returns

A newly created String object that holds the string value, or the string representation of value.

Properties

length

The number of characters in the string.

Methods

anchor()

Return a copy of the string, in an <A NAME=> environment.

big()

Return a copy of the string, in a <BIG> environment.

blink()

Return a copy of the string, in a <BLINK> environment.

bold()

Return a copy of the string, in a <B> environment.

charAt()

Extract the character at a given position from a string.

fixed()

Return a copy of the string, in a <TT> environment.

fontcolor()

Return a copy of the string, in a <FONT COLOR=> environment.

fontsize()

Return a copy of the string, in a <FONT SIZE=> environment.

indexOf()

Search the string for a character or substring.

italics()

Return a copy of the string, in a <I> environment.

lastIndexOf()

Search the string backwards for a character or substring.

link()

Return a copy of the string, in a <A HREF=> environment.

small()

Return a copy of the string, in a <SMALL> environment.

split()

Convert a string to an array of strings, using a specified delimiter character.

strike()

Return a copy of the string, in a <STRIKE> environment.

sub()

Return a copy of the string, in a <SUB> environment.

substring()

Extract a substring of a string.

sup()

Return a copy of the string, in a <SUP> environment.

toLowerCase()

Return a copy of the string, with all characters converted to lower case.

toUpperCase()

Return a copy of the string, with all characters converted to upper case.

Description

Strings are a basic data type in JavaScript. The String object type exists to provide methods for operating on 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, most of which simply make a copy of the string with HTML tags added before and after. Other methods, however, perform more interesting functions: extracting a character or a substring from the string, or searching for a character or a substring, for example.

Background Details

The string datatype and the String object are not the same. In Navigator 2.0, however, they are indistinguishable. In Navigator 3.0, you can use the typeof operator to distinguish them (a string has type "string" and a String object has type "object") but you can also use them interchangeably. The reason that string values and String objects can be used interchangeably is that JavaScript converts between these two types whenever necessary. When you invoke a String object method on a string value (which is not an object and cannot have methods), JavaScript converts that value to a temporary String object, allowing the method to be invoked. This temporary String object is not available to the program.

In Navigator 3.0, you can use the String object constructor method to create String objects that are not temporary, and that can actually be used by your programs. It is rarely, if ever, necessary to do this, but if you do, the String object you create can be used interchangeably with a string value. When an object is used where a string value is required, the object's toString() method is automatically invoked by JavaScript to convert the object to a string. If the object is a String object, the resulting value will be the string value that is required.

Example

A number of the String methods are used for creating HTML:

link_text =index.html "My Home Page".bold();
document.write(link_text.link("http://www.djf.com/~david"));

The code above embeds the following string into the HTML document that is currently being parsed:

<A HREF="http://www.djf.com/~david"><B>My Home Page</B></A>

Other methods of the String object perform more interesting functions. The following code, for example, extracts the 3rd through 5th characters of a string and converts them to uppercase letters:

s.substring(2,5).toUpperCase();

See Also

Chapter 3, Variables and Data Types


Previous Home Next
status Book Index String.anchor()

HTML: The Definitive Guide CGI Programming JavaScript: The Definitive Guide Programming Perl WebMaster in a Nutshell