JavaScript String Object

From Techotopia
Revision as of 20:22, 26 April 2007 by Neil (Talk | contribs) (JavaScript String Object Methods)

Jump to: navigation, search

The JavaScript String Object is one of the most useful of the JavaScript Core Objects. It provides a range of methods thaty can be used to perform a variety of string manipluation tasks (replacing parts of a string with different text, extreacting frgaments of a string, finding where a particular character appears in a string and much, much more).

Creating a JavaScript String Object

An instance of a String object is created just as any other object instance is created (See JavaScript Object Basics). The object is created using the new keyword with the string to be used as the property passed through as the argument. For example:


myString = new String("This is my string");

JavaScript String object methods and properties are accessed using the standard object dot notation:

objectName.property accesses a property. For example:


var stringLen = myString.length;

returns the length of the string and assigns it to the variable stringLen

Similarly:


var position = myString.indexOf ("my");

returns the postion index of the word my in the string (the word my begins position 8 because the first character is always index position 0, not index position 1).

JavaScript String Object Methods

The following table lists all the methods built into the String object class:

MethodDescription
big()Increases the font size of the text. Equivalent to wrapping the string in HTML and elements