JavaScript String object
String object
The String object is used to work with text (strings).
String object creation method: new String().
Syntax
var txt = new String("
string
");
Or a simpler way:
var txt = "
string
";
To learn the String object tutorial, please see JavaScript String object tutorial.
String object properties
| property | Description |
|---|---|
| constructor | A reference to the function that created the object. |
| length | The length of the string. |
| prototype | Allows you to add properties and methods to an object. |
String object methods
| Method | Description |
|---|---|
| charAt() | Returns the character at the specified position. |
| charCodeAt() | Returns the Unicode encoding of the character at the specified position. |
| concat() | Concatenates two or more strings and returns the new string. |
| endsWith() | Determines whether the current string ends with the specified substring (case-sensitive). |
| fromCharCode() | Converts a Unicode code point to a character. |
| indexOf() | Returns the position of the first occurrence of a specified string value in a string. |
| includes() | Determines whether a string contains a specified substring. |
| lastIndexOf() | Searches the string from back to front and returns the last occurrence of the string, counted from the starting position (0). |
| match() | Finds one or more matches of a regular expression. |
| repeat() | Copies a string a specified number of times and returns them concatenated together. |
| replace() | Searches a string for a matching substring and replaces the substring that matches the regular expression. |
| replaceAll() | Searches a string for matching substrings and replaces all substrings that match the regular expression. |
| search() | Finds a value that matches a regular expression. |
| slice() | Extracts a portion of a string and returns the extracted part in a new string. |
| split() | Splits a string into an array of strings. |
| startsWith() | Determines whether a string begins with a specified substring. |
| substr() | Extracts a specified number of characters from a string, starting at a given index. |
| substring() | Extracts the characters between two specified indices in a string. |
| toLowerCase() | Converts a string to lowercase. |
| toUpperCase() | Converts a string to uppercase. |
| trim() | Removes whitespace from both ends of a string. |
| toLocaleLowerCase() | Converts a string to lowercase according to the local host's language environment. |
| toLocaleUpperCase() | Converts a string to uppercase according to the local host's language environment. |
| valueOf() | Returns the primitive value of a string object. |
| toString() | Returns a string. |
String HTML wrapper methods
Returns the content wrapped in the corresponding HTML tags.
The following methods are not standard, so they may not be supported in some browsers.
| Method | Description |
|---|---|
| anchor() | Creates an HTML anchor. |
| big() | Displays a string in a large font. |
| blink() | Displays a blinking string. |
| bold() | Displays a string in bold. |
| fixed() | Displays a string as typewriter text. |
| fontcolor() | Displays a string in the specified color. |
| fontsize() | Displays a string in the specified size. |
| italics() | Displays a string in italics. |
| link() | Displays a string as a link. |
| small() | Displays a string in a small font. |
| strike() | Displays a string with a strikethrough. |
| sub() | Displays a string as a subscript. |
| sup() | Displays a string as a superscript. |