JavaScript » Objects » String

A String object, created using the String constructor, represents a series of characters in a string. The string can be enclosed between single or double quotes.

Examples

Code:
myString = new String("This is a string object")
Explanation:

This code shows the use of the String constructor to create a String object called 'myString'.

Properties

constructor

Syntax: Object.constructor

This specifies a function to create an object's property and is inherited by all objects from their prototype.

length

Syntax: object.length

This property returns the length of the string.

prototype

Syntax: Object.prototype.name = value

This allows the addition of properties and methods to any object.

Methods

anchor

Syntax: object.anchor("name")

This method is used to create an HTML anchor.

big

Syntax: object.'formatting_method'( )

This method displays the string using a big font, as if contained within HTML <BIG></BIG> tags.

blink

Syntax: object.'formatting_method'( )

This method makes the displayed string blink, as if contained within HTML <BLINK></BLINK> tags.

bold

Syntax: object.'formatting_method'( )

This method displays the string using a bold font, as if contained within HTML <B></B> tags.

charAt

Syntax: object.charAt(index)

This method returns a character from a string by referring to its index within that string.

charCodeAt

Syntax: object.charCodeAt(index)

This method returns a character's Unicode value from a string by referring to its index within that string.

concat

Syntax: object.concat(strName2, strName3....strName[n])

This method joins the text contained in one string with the text from other specified strings and returns a new string.

eval

Syntax: Object.eval(string)

The eval method is deprecated as a method of Object, but is still used as a high level function. It evaluates a string of JavaScript in the context of an object.

fixed

Syntax: object.'formatting_method'( )

This method displays the string using a fixed-pitch font, as if contained within HTML <TT></TT> tags.

fontcolor

Syntax: object.'formatting_method'( )

This method displays the string using a specified color, as if contained within HTML <FONT COLOR="color"></FONT> tags.

fontsize

Syntax: object.'formatting_method'( )

This method displays the string using a specified font size, as if contained within HTML <FONT SIZE="fontsize"></FONT> tags.

fromCharCode

Syntax: String.fromCharCode(num1,...,numN)

This method takes the specified Unicode values and returns a string (but not a String object).

indexOf

Syntax: object.indexOf(searchValue,[fromIndex])

When called from a String object, this method returns the index of the first occurance of the specified searchValue argument, starting from the specified fromIndex argument.

italics

Syntax: object.'formatting_method'( )

This method displays the string using italics, as if contained within HTML <I></I> tags.

lastIndexOf

Syntax: object.lastIndexOf(searchValue,[fromIndex])

When called from a String object, this method returns the index of the last occurance of the specified searchValue argument, searching backwards from the specified fromIndex argument.

link

Syntax: object.link("targetURL")

This method is used to create an HTML hyperlink in a document.

match

Syntax: object.match(regexp)

This method is used to match a specified regular expression against a string.

replace

Syntax: object.replace(regexp, newSubStr)
object.replace(regexp, function)

This method is used to match a specifed regular expression against a string and replace any match with a new substring.

search

Syntax: object.search(regexp)

This method is used to search for a match between a regular expression and the specified string.

slice

Syntax: object.slice(startSlice[, endSlice])

This method is used to 'slice' a section of a string and return a new string containing that section.

small

Syntax: object.'formatting_method'( )

This method displays the string using a small font, as if contained within HTML <SMALL></SMALL> tags.

split

Syntax: object.split [delimiter]

This method splits a string into substrings and creates an array containing the resulting substrings.

strike

Syntax: object.'formatting_method'( )

This method displays the string using struck-out text, as if contained within HTML <STRIKE></STRIKE> tags.

sub

Syntax: object.'formatting_method'( )

This method displays the string as subscript text, as if contained within HTML <SUB></SUB> tags.

substr

Syntax: object.substr(start[, length])

This method extracts the characters from a string beginning at the specified start index for the specified number of characters.

substring

Syntax: object.substring(indexA, indexB)

This method returns the characters in a string between two specified indices as a substring.

sup

Syntax: object.'formatting_method'( )

This method displays the string as superscript text, as if contained within HTML <SUP></SUP> tags.

toLowerCase

Syntax: object.toLowerCase( )

This method is used to convert the characters in a string to lower case.

toSource

Syntax: Object.toSource()

The toSource method returns a literal representing the source code of an object. This can then be used to create a new object.

toString

Syntax: Object.toString()

The toString method returns a string representing a specified object.

toUpperCase

Syntax: object.toUpperCase( )

This method is used to convert characters in a string to upper case.

unwatch

Syntax: Object.unwatch(property)

This method removes a watchpoint set for an object and property name with the watch method.

valueOf

Syntax: Object.valueOf()

This method returns a primitive value for a specified object.

watch

Syntax: Object.watch(property, handlerfunction)

This method adds a watchpoint to a property of the object.

See Also: