String Objects |
Active Web |
String objects are full script objects. String objects are created in two ways. The first way is by using the String constructor (see below). The second way is by creating a string primitive and then giving it a property, as this automatically converts the primitive to an object.
String literals are declared by using quotes. You can use either single or double quotes and if you need to use a quote character inside the string then use the escape character (\) immediately in front of the quote character. You use a string literal to create a string primitive.
var myString = "abcdef";
A new string primitive is created that has the value "abcdef".
The string value of an object is derived as shown in the table below:
Object Type |
String Value |
Undefined |
"undefined" |
Null |
"null" |
Boolean primitive |
The value is "true" if the boolean is true. The value is "false" if the boolean is false. |
Number primitive |
If the number is NaN, then the value is the string "NaN". If the number is +0 or -0, then the value is the string "0". If the number is infinity, then the value is the string "Infinity". For all numbers with less than 7 digits the value is the number converted to a 1 to 6 digit string. For all numbers of greater than 6 digits the value is the number converted to an exponential notation string. |
String primitive |
The value equals the input string (no conversion). |
Object |
The object is converted to a primitive and this is converted to a string using the rules above. |
String objects are of class String.
These properties are also available in string primitives.
Property |
Description |
length |
The number of characters in the string. This property does not enumerate and can not be changed or deleted. |
prototype |
A reference to the String Prototype. This property does not enumerate and can not be deleted, but it can be changed. |
The String object has no predefined functions.
The String constructor is used to create new string objects.
var myString = new String(arg);
A new string object is created using the argument. If the argument is not a string literal then the new string will be the result of calling the toString function on the argument.
When a string object is created it is given the Number and Boolean prototypes as well as the String prototype. This allows you to treat the string as if it were a number or a boolean. Shown below is the String object prototype chain.
When the String constructor is run as a function without the new operator it returns a string primitive that is the string resulting from converting the argument object to a string.
var myString = String(arg);
A new string primitive is created using the argument. If the argument is not a string literal then the new string will be the result of calling the toString function on the argument.
Constructor properties are read only (they can not be changed or deleted) and it is not possible to add new properties to the Constructor.
Property |
Description |
prototype |
A reference to the String Prototype. This property does not enumerate and can not be changed or deleted. |
Function |
Description |
fromCharCode([char0[, char1[, . . .]]]) |
Returns a string containing as many characters as the number of arguments. Each argument specifies one character of the resulting string, with the first argument specifying the first character, and so on, from left to right. An argument is converted to a character by first converting the argument to a 16 bit integer and then regarding the integer as the code point value of a character. If no arguments are supplied, the result is the empty string. Note: The ASCII code set is the first 127 characters of the code point value, so integers of less than 128 refer to ASCII characters. |
This prototype can be used by any object that has a string value available from the toString function.
Property |
Description |
prototype |
A reference to the Number Prototype. This property does not enumerate and can not be deleted but can be changed. |
The this reference used here always refers to the object that was used to run the function.
Except where explicitly stated otherwise all these functions are intentionally generic. They do not require that the this value or the arguments be String objects, and can therefore be applied to other objects where they will use the string value (see above) of the object.
Function |
Description |
charAt(pos) |
Returns a string containing the character at position pos in the string resulting from converting this object to a string. If there is no character at that position, the result is the empty string. |
charCodeAt(pos) |
Returns a number (a nonnegative integer less than 2 to power 16 ) representing the code point value of the character at position pos in the string resulting from converting this object to a string. If there is no character at that position, the result is NaN. |
compareTo(that) |
Compares two strings lexicographically. The comparison is based on the Unicode value of each character in the strings. The character sequence represented by converting this object to a string, is compared lexicographically to the character sequence represented by converting the that object to a string. The result is a negative integer if the this string lexicographically precedes the that string. The result is a positive integer if the this string lexicographically follows the that string. The result is zero if the strings are equal. |
concat ([string1[, string2[, . . .]]]) |
Returns a string consisting of the characters of this object (converted to a string) followed by the characters of each of string1, string2, etc. (where each argument is converted to a string). |
fromCharCode([char0[, char1[ , . . .]]]) |
DEPRECATED. This function has been moved to the String Constructor object. It is still available from the String Prototype for backward compatibility but may be removed in later releases. |
indexOf(searchstring[, position]) |
If searchString appears as a substring of the result of converting this object to a string, at one or more positions that are greater than or equal to position, then the index of the smallest such position is returned; otherwise, -1 is returned. If position is undefined, 0 is assumed, so as to search all of the string. |
lastIndexOf(searchstring[, position]) |
If searchString appears as a substring of the result of converting this object to a string at one or more positions that are smaller than or equal to position, then the index of the greatest such position is returned; otherwise, -1 is returned. If position is undefined, the length of the string value is assumed, so as to search all of the string. |
localeCompare(that) |
Returns the same as compareTo. This function is here so that in the future we can provide a Locale dependant version of compareTo. |
slice (start[, end]) |
The slice method takes two arguments, start and end, and returns a substring of the result of converting this object to a string, starting from character position start and running to, but not including, character position end (or through the end of the string if end is undefined). If start is negative, it is treated as (sourceLength+start) where sourceLength is the length of the string. If end is negative, it is treated as (sourceLength+end) where sourceLength is the length of the string. |
split (separator[, limit]) |
Returns an Array object into which substrings of the result of converting this object to a string have been stored. The substrings are determined by searching from left to right for occurrences of separator; these occurrences are not part of any substring in the returned array, but serve to divide up the string value. The value of separator may be a string of any length. If the this object is (or converts to) the empty string, the result depends on whether separator can match the empty string. If it can, the result array contains no elements. Otherwise, the result array contains one element, which is the empty string. If limit is not undefined, then the output array is truncated so that it contains no more than limit elements. |
substring (start[, end]) |
Returns a substring of the result of converting this object to a string, starting from character position start and running to, but not including, character position end of the string (or through the end of the string is end is undefined). |
toLowerCase() |
If this object is not already a string, it is converted to a string. The characters in that string are converted one by one to lower case. |
toLocaleLowerCase() |
Returns the same as toLowerCase. This function is here so that in the future we can provide a Locale dependant version of toLowerCase. |
toUpperCase |
If this object is not already a string, it is converted to a string. The characters in that string are converted one by one to upper case. Note: Because both toUpperCase and toLowerCase have context-sensitive behaviour, the functions are not symmetrical. In other words, s.toUpperCase().toLowerCase() is not necessarily equal to s.toLowerCase(). |
toLocaleUpperCase() |
Returns the same as toUpperCase. This function is here so that in the future we can provide a Locale dependant version of toUpperCase. |
valueOf() |
Returns a string derived from converting this object to a string. This function is not generic. If the this object is not a string object then this object is returned. |
Topic ID: 150097