String object
In JavaScript, the primitive string values (alias string literals) differ from the String object, their data type is string, not object. When you use strings in JavaScript, you work with primitive string values, not instances of the String object.
The JavaScript interpreter does not use the String object for implicit data type conversions (when a non-string value needs to be converted to a string, for example "32" + 2), but the String object can be used as a function to convert a value to a string literal explicitly. See Example 1 in the Some additional examples section for details.
When the JavaScript interpreter needs to convert a string literal to an object, it uses the String object. See Example 2 in the Some additional examples section for details.
Syntax:
Special characters:
In case of strings, the '\' (backslash) character has two different meanings, depending on the character that follows.
-
For an alphanumeric character, it indicates that it is a special character.
Not every alphabetical character has a special meaning after a backslash. For example, the "n" string contains one n character - the "\n" string contains one new line character, but the "a" and "\a" strings are identical, they contain one a character.
-
For a non-alphanumeric character, it indicates that it is a normal character.
It can be useful:if single and/or double quotes characters need to be used inside a string.
For example, the "\'\"" string contains a single and a double quote character.if backslash characters need to be used inside a string.
For example, the "\\" string contains one backslash character.if single and/or double quotes characters need to be used inside a string.
For example, the "\'\"" string contains a single and a double quote character.
\' | single quote (Apostrophe) |
\" | Double quote |
\\ | Backslash |
\b | Backspace |
\f | Form feed |
\n | New line |
\r | Carriage return |
\t | Tab |
\v | Vertical tab |
\ooo | Octal digits that identify a character in Latin-1 encoding, between 0 and 377. (For example "\256" is the copyright sign). |
\xhh | Where the h hexadecimal digits identify a character in Latin-1 encoding, between 00 and FF. (For example "\xAE" is the copyright sign). |
\uhhhh | Where the h hexadecimal digits identify a character in the Unicode character table. |
Some example:
- The "n" string contains one n character - the "\n" string contains one new line character.
- The "a" and "\a" strings are identical, they contain one a character.
- The "\'\"" string contains a single and a double quote character
- The "\\" string contains one backslash character.
- The
"123\
456" string is identical to the "123456" string. If you need to break a string into multiple lines in the source code, use the backslash character at the end of the lines, within the string. The new line character closes a string by default, but if a backslash character precedes it, then the JavaScript interpreter jumps over it. - The "\x4D" string is identical to the "M" string, because the hexadecimal 4D (decimal 77) is the code of the 'M' character.
- The "\061" string is identical to the "1" string, because the octal 61 (decimal 49) is the code of the '1' character.
- The "\61" string is identical to the "1" string.
- The "\612" string is identical to the "12" string, because the octal 612 is too large, but the octal 61 is allowed.
- The "\8" string is identical to the "8" string, because 8 is not an octal digit.
String actions:
|
|||
var str = "the text that the string will contain"; |
|||
|
|||
Did you find this example helpful?
|
|
|||
var str = "the text" + " that the string" + " will contain"; var str = "the text"; str = str + " that the string"; str = str + " will contain"; var str = "the text"; str += " that the string"; str += " will contain"; var str1 = "the text"; var str2 = " that the string will contain"; var str = str1 + str2; |
|||
|
|||
Did you find this example helpful?
|
|
|||
// number to string var str = String (56); // "56" var str = "" + 56; // "56" var str = (56).toString (); // "56" var str = (56).toString (2); // "111000" (binary) var str = (56).toString (15); // "38" (hexadecimal) var str = (56).toString (36); // "1k" (base of 36) // array to string var arr = [10, -2, 20]; var str = String (arr); // "10,-2,20" var str = "" + arr; // "10,-2,20" var str = arr.toString (); // "10,-2,20" var str = arr.join (" + "); // "10 + -2 + 20" // others var str = String (true); // "true" var str = "" + true; // "true" var str = String (false); // "false" var str = "" + false; // "false" var str = String (null); // "null" var str = "" + null; // "null" var str = String (undefined); // "undefined" var str = "" + undefined; // "undefined" |
|||
|
|||
Did you find this example helpful?
|
|
|||
// Number constructor var num = Number ("10"); // 10 var num = Number ("010"); // 10 var num = Number ("-32.12"); // -32.12 var num = Number ("12e-2"); // 0.12 (exponential notation) var num = Number ("0x10"); // 16 (hexadecimal base) var num = Number ("zero"); // NaN // Unary addition operator var num = +"10"; // 10 var num = +"010"; // 10 var num = +"-32.12"; // -32.12 var num = +"12e-2"; // 0.12 (exponential notation) var num = +"0x10"; // 16 (hexadecimal base) var num = +"zero"; // NaN |
|||
|
|||
Did you find this example helpful?
|
Members:
Properties:
Property | Support | Description | |||||
---|---|---|---|---|---|---|---|
length* | Returns the number of characters in the current String object. | ||||||
prototype | Returns a reference to the String.prototype object. The String.prototype object allows adding properties and methods to the String object that can be used with instances of the String object, like any predefined property or method. The prototype property is static, it cannot be accessed from an instance of the String object, only String.prototype is allowed. |
(*) - The property is inherits from the String.prototype.
Methods:
Method | Support | Description | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
[position] | Returns the character at the specified position. Unfortunately, Internet Explorer does not support the [] operator on strings, use the cross-browser charAt method instead. | |||||||||||
anchor (anchorName)* | Inserts the text content of the current String object into a named anchor element and returns the HTML source code of the tag. A named anchor element defines a destination anchor. | |||||||||||
big ()* | Inserts the text content of the current String object into a big element and returns the HTML source code of the tag. The big element specifies that the enclosed text should be displayed in a 'large' font. | |||||||||||
blink ()* | Inserts the text content of the current String object into a blink element and returns the HTML source code of the tag. The blink element specifies that the enclosed text should flash slowly. | |||||||||||
bold ()* | Inserts the text content of the current String object into a b or strong element and returns the HTML source code of the tag. The b and strong elements specify that the enclosed text should be displayed in bold. | |||||||||||
charAt (position)* | Returns the character at the specified position. | |||||||||||
charCodeAt (position)* |
Returns the Unicode encoding of the character at the specified position.
If you want to set the Unicode character code of a character in a string, use the fromCharCode method. |
|||||||||||
concat ([string1 [, string2 [, . . . [, stringN]]]])* | Appends the specified strings to the end of the current string and returns the concatenated string. The current String object is not changed. | |||||||||||
fixed ()* | Inserts the text content of the current String object into a tt element and returns the HTML source code of the tag. The tt element specifies that the enclosed text should be displayed in a fixed-width font. | |||||||||||
fontcolor (color)* | Inserts the text content of the current String object into a font element and returns the HTML source code of the tag. The fontcolor method also specifies the color property of the font element. A font element and its color property together specify that the enclosed text should be displayed in the given color. | |||||||||||
fontsize (size)* | Inserts the text content of the current String object into a font element and returns the HTML source code of the tag. The fontcolor method also specifies the size property of the font element. A font element and its size property together specify that the enclosed text should be displayed in the given size. | |||||||||||
fromCharCode ([code1 [, code2 [, ... [, codeN]]]]) |
Returns a concatenated string value from the given Unicode character codes.
The fromCharCode method is static, it cannot be accessed from an instance of the String object, only String.fromCharCode is allowed.
If you want to get the Unicode character code of a character in a string, use the charCodeAt method. |
|||||||||||
indexOf (subString [, startIndex])* | Searches for a substring from a position within the current String object and returns the zero-based position of the first match. If no match is found, it returns -1. The search is case sensitive. | |||||||||||
italics ()* | Inserts the text content of the current String object into an i element and returns the HTML source code of the tag. The i element specifies that the enclosed text should be displayed in italics. | |||||||||||
lastIndexOf (subString [, startIndex])* | Searches for a substring backwards from a position within the current String object and returns the zero-based position of the first match. If no match is found, it returns -1. The search is case sensitive. | |||||||||||
link (href)* | Inserts the text content of the current String object into an anchor element and returns the HTML source code of the tag. The anchor element displays the enclosed text as a hypertext link. | |||||||||||
localeCompare (stringToCompare)* | Compares the current string with the specified string based on the browser default language settings and returns the result. The localeCompare method returns -1 if the current string is before the stringToCompare, returns 1 if the current string is after the stringToCompare and returns 0 if the two strings are identical. The comparison is case sensitive. | |||||||||||
match (regExpToFind)* | Performs a regular expression search within the current string and returns the results in an Array. | |||||||||||
replace (regExpToFind, replaceTextOrFunc)* | Performs a regular expression search within the current string, replaces the matches with the string specified by the replaceTextOrFunc parameter and returns the result. The original string is not changed. | |||||||||||
search (regExpToFind)* | Performs a regular expression search within the current string and returns the zero-based position of the first match. If no match is found, returns -1. | |||||||||||
slice (startIndex, [endIndex])* | Returns the part of the current string between the given starting and end points. | |||||||||||
small ()* | Inserts the text content of the current String object into a small element and returns the HTML source code of the tag. The small element specifies that the enclosed text should be displayed in a smaller font. | |||||||||||
split ([separator [, limit]])* | Splits the current string into pieces at every occurrence of the given separator, and returns the result in an Array. | |||||||||||
strike ()* | Inserts the text content of the current String object into a strike element and returns the HTML source code of the tag. The strike element specifies that the enclosed text should be displayed in strike-through type. | |||||||||||
sub ()* | Inserts the text content of the current String object into a sub element and returns the HTML source code of the tag. The sub element specifies that the enclosed text should be displayed as subscripted text. | |||||||||||
substr (startIndex [, length])* | Returns a part of the current string from a specified position with limited length. | |||||||||||
substring (startIndex [, endIndex])* | Returns the part of the current string between the given start and end point. | |||||||||||
sup ()* | Inserts the text content of the current String object into a sup element and returns the HTML source code of the tag. The sup element specifies that the enclosed text should be displayed as superscript text. | |||||||||||
toLocaleLowerCase ()* | Returns a string that contains the text content of the current String object in lower-case. This method converts the alphabetic characters to lower-case based on the language settings of the browser. | |||||||||||
toLocaleUpperCase ()* | Returns a string that contains the text content of the current String object in upper-case. This method converts the alphabetic characters to upper-case based on the language settings of the browser. | |||||||||||
toLowerCase ()* | Returns a string that contains the text content of the current String object in lower-case. | |||||||||||
toSource ( )* | Returns a string representing the source code of the current string. | |||||||||||
toString ( )* | Returns a string representing the value of the current string. | |||||||||||
toUpperCase ()* | Returns a string that contains the text content of the current String object in upper-case. | |||||||||||
trim ()* |
|
Removes white spaces from the beginning and end of the current String object and returns the result. The original string is not changed. | ||||||||||
trimLeft ()* |
|
Removes white spaces from the beginning of the current String object and returns the result. The original string is not changed. | ||||||||||
trimRight ()* |
|
Removes white spaces from the end of the current String object and returns the result. The original string is not changed. | ||||||||||
valueOf ()* | Returns a string literal representing the value of the String object. |
(*) - The method is inherits from the String.prototype.
Properties
length
Returns the number of characters in the current String object.
|
|||||||||||||
prototype
Returns a reference to the String.prototype object.
The String.prototype object allows adding properties and methods to the String object
that can be used with instances of the String object, like any predefined property or method.
The prototype property is static, it cannot be accessed from an instance of the String object,
only String.prototype is allowed.
|
Methods
[position]
Returns the character at the specified position.
Unfortunately, Internet Explorer does not support the [] operator on strings, use the cross-browser charAt method instead.
Return value: String that contains the character. If the position is not valid, returns undefined.
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||
anchor (anchorName)
Inserts the text content of the current String object into a named anchor element and returns the HTML source code of the tag.
A named anchor element defines a destination anchor.
Return value: String that contains the HTML source code.
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||
big ()
Inserts the text content of the current String object into a big element and returns the HTML source code of the tag.
The big element specifies that the enclosed text should be displayed in a 'large' font.
Return value: String that contains the HTML source code.
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||
blink ()
Inserts the text content of the current String object into a blink element and returns the HTML source code of the tag.
The blink element specifies that the enclosed text should flash slowly.
Return value: String that contains the HTML source code.
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||
bold ()
Inserts the text content of the current String object into a b or strong element and returns the HTML source code of the tag.
The b and strong elements specify that the enclosed text should be displayed in bold.
Return value: String that contains the HTML source code.
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||
charAt (position)
Returns the character at the specified position.
Return value: String that contains the character. If the position is not valid, it returns an empty string.
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||
charCodeAt (position)
Returns the Unicode encoding of the character at the specified position.
Return value: Returns an integer that identifies the character code. If the position is not valid, it returns the NaN global property.
If you want to set the Unicode character code of a character in a string, use the fromCharCode method:
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||
concat ([string1 [, string2 [, . . . [, stringN]]]])
Appends the specified strings to the end of the current string and returns the concatenated string.
The current String object is not changed.
Return value: Returns the combined string.
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||
fixed ()
Inserts the text content of the current String object into a tt element and returns the HTML source code of the tag.
The tt element specifies that the enclosed text should be displayed in a fixed-width font.
Return value: String that contains the HTML source code.
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||
fontcolor (color)
Inserts the text content of the current String object into a font element and returns the HTML source code of the tag.
The fontcolor method also specifies the color property of the font element.
A font element and its color property together specify that the enclosed text should be displayed in the given color.
Return value: String that contains the HTML source code.
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||
fontsize (size)
Inserts the text content of the current String object into a font element and returns the HTML source code of the tag.
The fontcolor method also specifies the size property of the font element.
A font element and its size property together specify that the enclosed text should be displayed in the given size.
Return value: String that contains the HTML source code.
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||
fromCharCode ([code1 [, code2 [, ... [, codeN]]]])
Returns a concatenated string value from the given Unicode character codes.
The fromCharCode method is static, it cannot be accessed from an instance of the String object, only String.fromCharCode is allowed.
Return value: Returns the concatenated String.
How to set the Unicode character code of a character in a string:
If you want to get the Unicode character code of a character in a string, use the charCodeAt method:
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||
indexOf (subString [, startIndex])
Searches for a substring from a position within the current String object and returns the zero-based position of the first match.
If no match is found, it returns -1.
The search is case sensitive.
Return value: Integer, the position of the first occurrence, or -1 if no matching found.
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||
italics ()
Inserts the text content of the current String object into an i element and returns the HTML source code of the tag.
The i element specifies that the enclosed text should be displayed in italics.
Return value: String that contains the HTML source code.
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||
lastIndexOf (subString [, startIndex])
Searches for a substring backwards from a position within the current String object and returns the zero-based position of the first match.
If no match is found, it returns -1.
The search is case sensitive.
Return value: Integer, the position of the last occurrence, or -1 if no match is found.
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||
link (href)
Inserts the text content of the current String object into an anchor element and returns the HTML source code of the tag.
The anchor element displays the enclosed text as a hypertext link.
Return value: String that contains the HTML source code.
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||
localeCompare (stringToCompare)
Compares the current string with the specified string based on the browser default language settings and returns the result.
The localeCompare method returns -1 if the current string is before the stringToCompare, returns 1 if the current string is after the stringToCompare and returns 0 if the two strings are identical.
The comparison is case sensitive.
Return value: Returns -1, 0 or 1.
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||
match (regExpToFind)
Performs a regular expression search within the current string and returns the results in an Array.
Return value: Returns an Array of matching strings, or null if no match is found..
This example show how to find negative integers in a string:
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||
replace (regExpToFind, replaceTextOrFunc)
Performs a regular expression search within the current string, replaces the matches with the string specified by the replaceTextOrFunc parameter and returns the result.
The original string is not changed.
Return value: Returns the replaced string.
Case-sensitive search:
Case-insensitive search:
Searching and replacing all occurences:
Removing all uppercase letters:
Using the $n symbols to convert a date to another format:
Using a funtion as a second parameter of the replace method to perform arithmetic operations in a string:
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||
search (regExpToFind)
Performs a regular expression search within the current string and returns the zero-based position of the first match.
If no match is found, returns -1.
Note that the exec and test methods of the RegExp object provides similar functionality.
Return value: Returns the position of the first match.
Case-sensitive search:
Case-insensitive search:
Get the longest prefix of a string that only contains alphabetical characters:
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||
slice (startIndex, [endIndex])
Returns the part of the current string between the given starting and end points.
Return value: Returns the substring.
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||
small ()
Inserts the text content of the current String object into a small element and returns the HTML source code of the tag.
The small element specifies that the enclosed text should be displayed in a smaller font.
Return value: String that contains the HTML source code.
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||
split ([separator [, limit]])
Splits the current string into pieces at every occurrence of the given separator, and returns the result in an Array.
The split pieces do not contain the substrings matched by the separator.
Return value: Returns an Array of split strings.
The returned Array contains both empty and non-empty pieces except when the split pattern is a regular expression in Internet Explorer before version 9.
In that cases, the returned Array does not contain the empty pieces. See the examples below for details.
A commonly-used split operation:
The split method works differently in Internet Explorer before version 9 than in other browsers:
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||
strike ()
Inserts the text content of the current String object into a strike element and returns the HTML source code of the tag.
The strike element specifies that the enclosed text should be displayed in strike-through type.
Return value: String that contains the HTML source code.
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||
sub ()
Inserts the text content of the current String object into a sub element and returns the HTML source code of the tag.
The sub element specifies that the enclosed text should be displayed as subscripted text.
Return value: String that contains the HTML source code.
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||
substr (startIndex [, length])
Returns a part of the current string from a specified position with limited length.
Return value: Returns the substring.
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||
substring (startIndex [, endIndex])
Returns the part of the current string between the given starting and end points.
Return value: Returns the substring.
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||
sup ()
Inserts the text content of the current String object into a sup element and returns the HTML source code of the tag.
The sup element specifies that the enclosed text should be displayed as superscript text.
Return value: String that contains the HTML source code.
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||
toLocaleLowerCase ()
Returns a string that contains the text content of the current String object in lower-case.
This method converts alphabetic characters to lower-case based on the language settings of the browser.
Return value: Returns the lower-case string.
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||
toLocaleUpperCase ()
Returns a string that contains the text content of the current String object in upper-case.
This method converts alphabetic characters to upper-case based on the language settings of the browser.
Return value: Returns the upper-case string.
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||
toLowerCase ()
Returns a string that contains the text content of the current String object in lower-case.
Return value: Returns the lower-case string.
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||
toSource ()
Returns a string representing the source code of the current string.
Return value: Returns a string representing the source code.
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||
toString ()
Returns a string representing the value of the current string.
Return value: Returns a string representing the value.
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||
toUpperCase ()
Returns a string that contains the text content of the current String object in upper-case.
Return value: Returns the upper-case string.
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||
trim ()
Removes white spaces from the beginning and end of the current String object and returns the result.
The original string is not changed.
Return value: Returns the trimmed string.
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||
trimLeft ()
Removes white spaces from the beginning of the current String object and returns the result.
The original string is not changed.
Return value: Returns the trimmed string.
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||
trimRight ()
Removes white spaces from the end of the current String object and returns the result.
The original string is not changed.
Return value: Returns the trimmed string.
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||
valueOf ()
Returns a string literal representing the value of the String object.
Return value: Returns a string literal.
|
Some additional examples:
Example 1:
Converts a value to a string literal explicitly with the String function:
Example 2:
Adding a new method to the String object and calling it for a string literal:
|