String Object (JavaScript)
Allows manipulation and formatting of text strings and determination and location of substrings within strings.
newString = new String(["stringLiteral"])
JavaScript provides escape sequences that you can include in strings to create characters that you cannot type directly. For example, \t specifies a tab character. For more information, see Special Characters (JavaScript).
String Literals
A string literal is zero or more characters enclosed in single or double quotation marks. A string literal has a primary (primitive) data type of string. A String object is created by using the new Operator, and has a data type of Object.
The following example shows that the data type of a string literal is not the same as that of a String object.
var strLit = "This is a string literal."; var strObj = new String("This is a string object."); document.write(typeof strLit); document.write("<br/>"); document.write(typeof strObj); // Output: // string // object
Methods for String Literals
When you call a method on a string literal, it is temporarily converted to a string wrapper object. The string literal is treated as though the new operator were used to create it.
The following example applies the toUpperCase method to a string literal.
var strLit = "This is a string literal."; var result1 = strLit.toUpperCase(); var result2 = (new String(strLit)).toUpperCase(); document.write(result1); document.write("<br/>"); document.write(result2); // Output: // THIS IS A STRING LITERAL. // THIS IS A STRING LITERAL.
Accessing an Individual Character
You can access an individual character of a string as a read-only array-indexed property. This feature was introduced in Internet Explorer 9 standards mode, Internet Explorer 10 standards mode, and win8_appname_long apps. The following example accesses individual string characters.
var str = "abcd"; var result = str[2]; document.write (result); // Output: c var result = "the"[0]; document.write(result); // Output: t
The following table lists the methods of the String object.
|
Method |
Description |
|---|---|
|
Places an HTML anchor that has a NAME attribute around text. |
|
|
Places HTML <BIG> tags around text. |
|
|
Places HTML <BLINK> tags around text. |
|
|
Places HTML <B> tags around text. |
|
|
Returns the character at the specified index. |
|
|
Returns the Unicode encoding of the specified character. |
|
|
Returns a string that contains the concatenation of two supplied strings. |
|
|
Places HTML <TT> tags around text. |
|
|
Places HTML <FONT> tags with a COLOR attribute around text. |
|
|
Places HTML <FONT> tags with a SIZE attribute around text. |
|
|
Returns a Boolean value that indicates whether an object has a property with the specified name. |
|
|
Returns the character position where the first occurrence of a substring occurs within a string. |
|
|
Returns a Boolean value that indicates whether an object exists in another object's prototype chain. |
|
|
Places HTML <I> tags around text. |
|
|
Returns the last occurrence of a substring within a string. |
|
|
Places an HTML anchor that has an HREF attribute around text. |
|
|
Returns a value that indicates whether two strings are equivalent in the current locale. |
|
|
Searches a string by using a supplied Regular Expression object and returns the results as an array. |
|
|
Returns a Boolean value that indicates whether a specified property is part of an object and whether it is enumerable. |
|
|
Uses a regular expression to replace text in a string and returns the result. |
|
|
Returns the position of the first substring match in a regular expression search. |
|
|
Returns a section of a string. |
|
|
Places HTML <SMALL> tags around text. |
|
|
Returns the array of strings that results when a string is separated into substrings. |
|
|
Places HTML <STRIKE> tags around text. |
|
|
Places HTML <SUB> tags around text. |
|
|
Returns a substring beginning at a specified location and having a specified length. |
|
|
Returns the substring at a specified location within a String object. |
|
|
Places HTML <SUP> tags around text. |
|
|
Returns a string in which all alphabetic characters are converted to lowercase, taking into account the host environment's current locale. |
|
|
Returns an object converted to a string, using the current locale. |
|
|
Returns a string in which all alphabetic characters are converted to uppercase, taking into account the host environment's current locale. |
|
|
Returns a string in which all alphabetic characters are converted to lowercase. |
|
|
Returns the string. |
|
|
Returns a string in which all alphabetic characters are converted to uppercase. |
|
|
Returns a string with leading and trailing white space and line terminator characters removed. |
|
|
Returns the string. |