please dont rip this site

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

file: /Techref/language/java/script/definitive/refp_354.htm, 11KB, , updated: 2019/10/14 15:00, local time: 2024/11/27 11:55,
TOP NEW HELP FIND: 
3.140.196.5:LOG IN

 ©2024 These pages are served without commercial sponsorship. (No popup ads, etc...).Bandwidth abuse increases hosting cost forcing sponsorship or shutdown. This server aggressively defends against automated copying for any reason including offline viewing, duplication, etc... Please respect this requirement and DO NOT RIP THIS SITE. Questions?
Please DO link to this page! Digg it! / MAKE!

<A HREF="http://techref.massmind.org/Techref/language/java/script/definitive/refp_354.htm"> [Chapter 21] Reference: String</A>

After you find an appropriate page, you are invited to your to this massmind site! (posts will be visible only to you before review) Just type a nice message (short messages are blocked as spam) in the box and press the Post button. (HTML welcomed, but not the <A tag: Instead, use the link box to link to another page. A tutorial is available Members can login to post directly, become page editors, and be credited for their posts.


Link? Put it here: 
if you want a response, please enter your email address: 
Attn spammers: All posts are reviewed before being made visible to anyone other than the poster.
Did you find what you needed?

 

Welcome to massmind.org!

 

Welcome to techref.massmind.org!

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  .