The String Object


The String Object

We've worked with strings throughout the book, but we haven't dealt with the String object formally yet. In fact, when you use strings in variables , they're automatically converted into String objects in JavaScript:

 var text = "Now is the time." 

You also can use the String constructor to create a String object:

 var text = new String("Now is the time.") 

Tip

The strings you assign to simple variables in JavaScript aren't actually treated as full String objectsyou can't add properties and methods to them, for example, which you can with full String objects. More on that in Chapter 23, "Cookies and Creating Your Own Objects."


You can find the properties and methods of the String object in Table 18.2, its properties in depth in Table 18.3, and its methods in depth in Table 18.4.

Table 18.2. The Properties and Methods of the String Object

Properties

Methods

constructor

anchor

length

big

prototype

blink

 

bold

 

charAt

 

charCodeAt

 

concat

 

fixed

 

fontcolor

 

fontsize

 

fromCharCode

 

indexOf

 

italics

 

lastIndexOf

 

link

 

localeCompare

 

match

 

replace

 

search

 

slice

 

small

 

split

 

strike

 

sub

 

substr

 

substring

 

sup

 

toLocaleLowerCase

 

toLocaleUpperCase

 

toLowerCase

 

toString

 

toUpperCase

 

valueOf

Table 18.3. The Properties of the String Object

Property

NS2

NS3

NS4

NS6

IE3a

IE3b

IE4

IE5

IE5.5

IE6

constructor

   

x

x

   

x

x

x

x

 

Read/write

 

This property specifies the function that creates an object.

length

x

x

x

x

x

x

x

x

x

x

 

Read-only

 

This property holds the length of the text in the string, measured in characters .

prototype

 

x

x

x

   

x

x

x

x

 

Read/write

 

This property holds a reference to the String object's prototype.

Table 18.4. The Methods of the String Object

Method

NS2

NS3

NS4

NS6

IE3a

IE3b

IE4

IE5

IE5.5

IE6

anchor

x

x

x

x

x

x

x

x

x

x

 

Returns: String

 

This method returns a string with an HTML anchor with a NAME attribute around specified text in the object.

 

Syntax: string .anchor( text ) , where text is the text you want to place in the NAME attribute of the HTML anchor.

big

x

x

x

x

x

x

x

x

x

x

 

Returns: String

 

This method changes the text to big text by surrounding it with HTML <BIG> tags.

 

Syntax: string .big() . See "Formatting Text" in this chapter.

blink

x

x

x

x

x

x

x

x

x

x

 

This method changes the text to blinking text by surrounding it with HTML <BLINK> tags.

 

Syntax: string .blink() . See "Formatting Text" in this chapter.

bold

x

x

x

x

x

x

x

x

x

x

 

This method changes the text to big text by surrounding it with HTML <B> tags.

 

Syntax: string .bold() . See "Formatting Text" in this chapter.

charAt

x

x

x

x

x

x

x

x

x

x

 

Returns: Single character

 

This method returns the character at the specified index of a String object.

 

Syntax: string .charAt( index ) , where index is the index of the character you want.

charCodeAt

   

x

x

   

x

x

x

x

 

Returns: Integer character code

 

This method returns the character code of the character at the specified index of a String object.

 

Syntax: string .charCodeAt( index ) , where index is the index of the character whose code you want.

concat

   

x

x

   

x

x

x

x

 

Returns: String

 

Returns a string value containing the concatenation of the current string with any given string(s). Sytnax: string .concat([ string1 [, ... [, stringN ]]]]) .

fixed

x

x

x

x

x

x

x

x

x

x

 

This method changes the text to fixed-width text by surrounding it with HTML <TT> tags.

 

Syntax: string .fixed() . See "Formatting Text" in this chapter.

fontcolor

x

x

x

x

x

x

x

x

x

x

 

This method sets font color by adding <FONT> tags.

 

Syntax: string .fontcolor( color ) , where color is the new color (either a color triplet or a color name recognized by the browser).

fontsize

x

x

x

x

x

x

x

x

x

x

 

This method sets font size by adding <FONT> tags.

 

Syntax: string .fontsize( size ) , where size is the an integer for the <FONT> tag's SIZE attribute.

fromCharCode

   

x

x

   

x

x

x

x

 

Returns: String

 

This method returns a string from a number of character values.

 

Syntax: string .fromCharCode([ code1 [, ... [, codeN ]]]]).

indexOf

x

x

x

x

x

x

x

x

x

x

 

Returns: Integer

 

This method the character position where the first occurrence of a substring occurs within a String object.

 

Syntax: string .indexOf( subString ), where subString is the string you're searching for. See "Searching Strings" in this chapter.

italics

x

x

x

x

x

x

x

x

x

x

 

This method changes the text to italics by surrounding it with HTML <I> tags.

 

Syntax: string .italics() . See "Formatting Text" in this chapter.

lastIndexOf

x

x

x

x

x

x

x

x

x

x

 

Returns: Integer

 

This method the character position where the last occurrence of a substring occurs within a String object.

 

Syntax: string .lastIndexOf( subString ), where subString is the string for which you're searching. See "Searching Strings" in this chapter.

link

x

x

x

x

x

x

x

x

x

x

 

This method returns as string with an HTML anchor and an HREF attribute around the text in a String object.

 

Syntax: string .link( href ) , where href is the URL the hyperlink should point to.

localeCompare

     

x

       

x

x

 

Returns: Integer

 

This method performs a locale-sensitive string comparison of strings.

 

Returns -1, 0, or +1, depending on the sort order of the system default locale.

 

Syntax: string1 .localeCompare( string2 ) . If string1 sorts before string2 , returns -1; if string1 sorts after string2 , returns 1. Returning 0 means that the two strings are equal.

match

x

x

x

x

x

x

x

x

x

x

 

Returns: Array of strings

 

This method executes a regular expression match on the string.

 

Syntax: string .match( regExp ) . Returns an array of matches or null if there were no matches. See Chapter 20 for more on regular expressions.

replace

   

x

x

   

x

x

x

x

 

Returns: New string

 

This method replaces substrings with a new string.

 

Syntax: string.replace( regExp , replacementText ) , where regExp is a regular expression and replacementText replaces matches to regExp (all matches are replaced if regExp includes the /g modifier). See "Searching and Replacing" in this chapter for an example.

search

   

x

x

   

x

x

x

x

 

Returns: Integer

 

This method returns the position of the first substring match in a regular expression search.

 

Syntax: string .search( regExp ) , where regExp is the regular expression to match.

slice

   

x

x

   

x

x

x

x

 

Returns: String

 

This method returns a slicethat is, a sectionof a string.

 

Syntax: string .slice( start , end ) , where start and end are the indices of the start and end points of the slice.

small

x

x

x

x

x

x

x

x

x

x

 

This method changes the text to small text by surrounding it HTML <SMALL> tags.

 

Syntax: string .small() . See "Formatting Text" in this chapter.

split

 

x

x

x

   

x

x

x

x

 

Returns: Array of strings

 

This method splits a string. Returns the array of strings that results when a string is separated into substrings.

 

Syntax: string .split( separator [, max ]) , where separator is the delimiter (such as "," ) or regular expression to split into substrings, and max is the maximum number of substrings to create. See "Splitting Strings" in this chapter.

strike

x

x

x

x

x

x

x

x

x

x

 

This method changes the text to struck-out text by surrounding it with HTML <STRIKE> tags.

 

Syntax: string .strike() . See "Formatting Text" in this chapter.

sub

x

x

x

x

x

x

x

x

x

x

 

This method changes the text to subscript text by surrounding it with HTML <SUB> tags.

 

Syntax: string .sub() . See "Formatting Text" in this chapter.

substr

   

x

x

   

x

x

x

x

 

Returns: String

 

This method returns a substring beginning at a specified location and having a given length.

 

Syntax: string .substr( start [, length ) ; where start is the beginning index of the substring, and length is the length of the substring.

substring

x

x

x

x

x

x

x

x

x

x

 

Returns: String

 

This method returns the substring at the specified location within a String object.

 

Syntax: string .substring( start [, end ) ; where start is the beginning index of the substring, and end is the ending index of the substring.

sup

x

x

x

x

x

x

x

x

x

x

 

This method changes the text to superscript text by surrounding it with HTML <SUP> tags.

 

Syntax: string .sup() . See "Formatting Text" in this chapter.

toLocaleLowerCase

     

x

       

x

x

 

Returns: String

 

This method converts the string to locale-sensitive lower case.

 

Syntax: string .toLocaleLowerCase() .

toLocaleUpperCase

     

x

       

x

x

 

Returns: String

 

This method converts the string to locale-sensitive upper case.

 

Syntax: string .toLocaleUpperCase() .

toLowerCase

x

x

x

x

x

x

x

x

x

x

 

Returns: String

 

This method converts the string to lower case.

 

Syntax: string .toLowerCase() . See "Changing Case" in this chapter.

toString

   

x

x

   

x

x

x

x

 

Returns: String

 

This method just returns the string itself.

 

Syntax: string .toString() .

toUpperCase

x

x

x

x

x

x

x

x

x

x

 

Returns: String

 

This method converts the string to upper case.

 

Syntax: string .toUpperCase() . See "Changing Case" in this chapter.

valueOf

   

x

x

   

x

x

x

x

 

Returns: String

 

This method just returns the string itself.

 

Syntax: string .valueOf() .



Inside Javascript
Inside JavaScript
ISBN: 0735712859
EAN: 2147483647
Year: 2005
Pages: 492
Authors: Steve Holzner

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net