Difference between revisions of "JavaScript String Object"

From Techotopia
Jump to: navigation, search
(JavaScript String Object Methods)
(JavaScript String Object Methods)
Line 76: Line 76:
 
<tr>
 
<tr>
 
<td>match()<td>Accepts a regular expression as an argument and returns an array of matches found in the String object instance.</td>
 
<td>match()<td>Accepts a regular expression as an argument and returns an array of matches found in the String object instance.</td>
 +
</tr>
 +
<tr>
 +
<td>replace()<td>Accepts two arguments: a regular expression and a replacement string. Converts all matches to the regular expression in a String object instance with tyhe replacement string. Returns -1 if no match is found.</td>
 
</tr>
 
</tr>
 
</table>
 
</table>

Revision as of 14:15, 27 April 2007

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 the most commonly used methods built into the String object class:

MethodDescription
big()Increases the font size of the text. Equivalent to wrapping the string in HTML <big> and </big> elements (e.g. Example)
bold()Makes the string bold. Equivalent to wrapping the string in HTML <b> and </b> elements (e.g Example)
charAt()Returns the character at the specified indexs into the string. It is important to remember that the first character in a string in at index 0, not index 1.
concat()Takes two strings as auguments and combines them into a single string. Returns the combined string.
fixed()Coverts the string to be displayed in fixed pitch (teletype style) font. Equivalent to wrapping the string in HTML <tt> and </tt> elements (e.g. Example)
fontcolor()Specifies the color of the string. Equivalent to wrapping the string in HTML <font color="color"> and </font> elements (e.g. Example)
fontsize()Specifies the color of the string. Equivalent to wrapping the string in HTML <font size="size"> and </font> elements (e.g. Example)
indexOf()Takes a string as an argument and returns the index of the first occurance of that string in an instance of a String object. Returns -1 if no match to the string is found.
italics()Coverts the string to be displayed in italics. Equivalent to wrapping the string in HTML <i> and </i> elements (e.g. Example)
lastIndexOf()Takes a string as an argument and returns the index of the last occurance of that string in an instance of a String object. Returns -1 if no match to the string is found.
link()Accepts a URL as an argument and constructs an <a> tag using the URL as the href attribute.
match()Accepts a regular expression as an argument and returns an array of matches found in the String object instance.
replace()Accepts two arguments: a regular expression and a replacement string. Converts all matches to the regular expression in a String object instance with tyhe replacement string. Returns -1 if no match is found.