String

An object representing a series of characters in a string.

Core object

Created by

The String constructor:
new String( string )

Parameters

Parameter Description
string Any string.

Description

The String object is a wrapper around the string primitive data type. Do not confuse a string literal with the String object. For example, the following code creates the string literal s1 and also the String object s2:

You can call any of the methods of the String object on a string literal value—MetaScript automatically converts the string literal to a temporary String object, calls the method, then discards the temporary String object. You can also use the String.length property with a string literal.

You should use string literals unless you specifically need to use a String object, because String objects can have counterintuitive behavior. For example:

A string can be represented as a literal enclosed by single or double quotation marks; for example, "MetaCommunications" or 'MetaCommunications'.
You can convert the value of any object into a string using the top-level String function.

Property Summary

Property Description
constructor Specifies the function that creates an object's prototype.
length Reflects the length of the string.
prototype Allows the addition of properties to a String object.

Method Summary

Method Description
charAt Returns the character at the specified index.
charCodeAt Returns a number indicating the Unicode value of the character at the given index.
concat Combines the text of two strings and returns a new string.
fromCharCode Returns a string created by using the specified sequence of Unicode values.
indexOf Returns the index within the calling String object of the first occurrence of the specified value, or -1 if not found.
lastIndexOf Returns the index within the calling String object of the last occurrence of the specified value, or -1 if not found.
match Used to match a regular expression against a string.
replace Used to find a match between a regular expression and a string, and to replace the matched substring with a new substring.
search Executes the search for a match between a regular expression and a specified string.
slice Extracts a section of a string and returns a new string.
split Splits a String object into an array of strings by separating the string into substrings.
substr Returns the characters in a string beginning at the specified location through the specified number of characters.
substring Returns the characters in a string between two indexes into the string.
toLowerCase Returns the calling string value converted to lowercase.
toSource Returns an object literal representing the specified object; you can use this value to create a new object. Overrides the Object.toSource method.
toString Returns a string representing the specified object. Overrides the Object.toString method.
toUpperCase Returns the calling string value converted to uppercase.
trim Returns the calling string value with whitespace from both ends of the string.
valueOf Returns the primitive value of the specified object. Overrides the Object.valueOf method.

Examples

Example 1: String literal. The following statement creates a string literal:

Example 2: String literal properties. The following statements evaluate to 8, "SCHAEFER," and "schaefer":

Example 3: Accessing individual characters in a string. You can think of a string as an array of characters. In this way, you can access the individual characters in the string by indexing that array. For example, the following code displays "The first character in the string is H":

Enter labels to add to this page:
Please wait 
Looking for a label? Just start typing.