| title | ms.custom | ms.date | ms.prod | ms.reviewer | ms.suite | ms.technology | ms.tgt_pltfrm | ms.topic | f1_keywords | dev_langs | helpviewer_keywords | ms.assetid | caps.latest.revision | author | ms.author | manager | |||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
String Object (JavaScript) | Microsoft Docs |
01/18/2017 |
windows-client-threshold |
|
language-reference |
|
|
|
8063ecd5-5778-4e87-b985-b21420171914 |
32 |
mikejo5000 |
mikejo |
ghogen |
String Object (JavaScript)
Allows manipulation and formatting of text strings and determination and location of substrings within strings.
Syntax
newString = new String(["stringLiteral"])
Parameters
newString
Required. The variable name to which the String object is assigned.
stringLiteral
Optional. Any group of Unicode characters.
Remarks
[!INCLUDEjavascript] 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.
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 [!INCLUDEjsv9text]. 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 Requirements
The String Object was introduced in [!INCLUDEjsv1text]. Some members in the following lists were introduced in later versions.
Properties
The following table lists the properties of the String object.
| Property | Description |
|---|---|
| constructor Property | Specifies the function that creates an object. |
| length Property (String) | Returns the length of a String object. |
| prototype Property | Returns a reference to the prototype for a class of objects. |
Functions
The following table lists the functions of the String object.
| Function | Description |
|---|---|
| String.fromCharCode Function | Returns a string from a number of Unicode character values. |
| String.fromCodePoint Function | Returns the string associated with a Unicode UTF-16 code point. |
| String.raw Function | Returns the raw string form of a template string. |
Methods
The following table lists the methods of the String object.
| Method | Description |
|---|---|
| anchor Method | Places an HTML anchor that has a NAME attribute around text. |
| big Method | Places HTML <BIG> tags around text. |
| blink Method | Places HTML <BLINK> tags around text. |
| bold Method | Places HTML <B> tags around text. |
| charAt Method | Returns the character at the specified index. |
| charCodeAt Method | Returns the Unicode encoding of the specified character. |
| codePointAt Method | Returns the code point for a Unicode UTF-16 character. |
| concat Method (String) | Returns a string that contains the concatenation of two supplied strings. |
| endsWith Method | Returns a Boolean value that indicates whether a string or substring ends with the passed in string. |
| includes Method | Returns a Boolean value that indicates whether the passed in string is contained in the string object. |
| fixed Method | Places HTML <TT> tags around text. |
| fontcolor Method | Places HTML <FONT> tags with a COLOR attribute around text. |
| fontsize Method | Places HTML <FONT> tags with a SIZE attribute around text. |
| hasOwnProperty Method | Returns a Boolean value that indicates whether an object has a property with the specified name. |
| indexOf Method (String) | Returns the character position where the first occurrence of a substring occurs within a string. |
| isPrototypeOf Method | Returns a Boolean value that indicates whether an object exists in another object's prototype chain. |
| italics Method | Places HTML <I> tags around text. |
| lastIndexOf Method (String) | Returns the last occurrence of a substring within a string. |
| link Method | Places an HTML anchor that has an HREF attribute around text. |
| localeCompare Method | Returns a value that indicates whether two strings are equivalent in the current locale. |
| match Method | Searches a string by using a supplied Regular Expression object and returns the results as an array. |
| normalize Method | Returns the Unicode Normalization Form of a specified string. |
| propertyIsEnumerable Method | Returns a Boolean value that indicates whether a specified property is part of an object and whether it is enumerable. |
| repeat Method | Returns a new string object with a value equal to the original string repeated the specified number of times. |
| replace Method | Uses a regular expression to replace text in a string and returns the result. |
| search Method | Returns the position of the first substring match in a regular expression search. |
| slice Method (String) | Returns a section of a string. |
| small Method | Places HTML <SMALL> tags around text. |
| split Method | Returns the array of strings that results when a string is separated into substrings. |
| startsWith Method | Returns a Boolean value that indicates whether a string or substring starts with the passed in string. |
| strike Method | Places HTML <STRIKE> tags around text. |
| sub Method | Places HTML <SUB> tags around text. |
| substr Method | Returns a substring beginning at a specified location and having a specified length. |
| substring Method | Returns the substring at a specified location within a String object. |
| sup Method | Places HTML <SUP> tags around text. |
| toLocaleLowerCase Method | Returns a string in which all alphabetic characters are converted to lowercase, taking into account the host environment's current locale. |
| toLocaleString Method | Returns an object converted to a string, using the current locale. |
| toLocaleUpperCase Method | Returns a string in which all alphabetic characters are converted to uppercase, taking into account the host environment's current locale. |
| toLowerCase Method | Returns a string in which all alphabetic characters are converted to lowercase. |
| toString Method | Returns the string. |
| toUpperCase Method | Returns a string in which all alphabetic characters are converted to uppercase. |
| trim Method | Returns a string with leading and trailing white space and line terminator characters removed. |
| valueOf Method | Returns the string. |