JavaScript Introduction | Standard Objects | String Methods
String Methods
String methods are methods used for string-related operations defined on the String object.
- String.fromCharCode()
- String.fromCodePoint()
String.fromCharCode() Method
This method returns a string composed of characters corresponding to a series of comma-separated Unicode values.
String.fromCharCode(65, 66, 67); // "ABC"
String.fromCodePoint() Method
This method returns a string composed of characters corresponding to a series of comma-separated code points.
String.fromCodePoint(65, 66, 67); // "ABC"
String.fromCodePoint(0x2F804); // "\uD87E\uDC04"
String.fromCodePoint(194564); // "\uD87E\uDC04"
The String.fromCodePoint() method is not supported in Safari or Internet Explorer.
JavaScript String Methods
| Method | Description |
|---|---|
| String.fromCharCode() | Returns a string composed of characters corresponding to a series of comma-separated Unicode values. |
| String.fromCodePoint() | Returns a string composed of characters corresponding to a series of comma-separated code points. |
| String.raw() | Returns the raw form of a template string. |
String.prototype Methods
Every String instance inherits methods and properties from String.prototype.
Using these inherited String.prototype methods, you can perform the following string operations.
- Return positions in a string
- Return the character at a specified position in a string
- Extract strings
- Split strings
- Concatenate strings
- Convert string case
- Remove whitespace around strings
- Manipulate strings using regular expressions
Because the value of a String instance is immutable, every String method creates and returns a new string as its result.
Finding Positions in a String
The following methods return the position where a specific character or string first appears, or the position where it last appears, in a String instance.
- indexOf()
- lastIndexOf()
These methods can receive the position in the String instance where the search should start as the second argument.
If the specified character or string cannot be found, they return -1.
var str = "abcDEFabc";
str.indexOf('abc'); // 0 -> JavaScript indexes start at 0.
str.indexOf('abcd'); // -1 -> character case is distinguished when comparing strings.
str.indexOf('abc', 3); // 6 -> starts searching for 'abc' from index 3.
str.lastIndexOf('abc'); // 6
str.lastIndexOf('d'); // -1
str.lastIndexOf('c'); // 8
Returning the Character at a Specified Position
The following methods return the character or character code at the passed index in a String instance.
- charAt()
- charCodeAt()
- codePointAt()
var str = "abcDEFabc";
str.charAt(0); // a
str.charAt(10); // empty string -> returns an empty string if the passed index is greater than the string length.
str.charCodeAt(0); // 97 -> returns the UTF-16 code for 'a'.
str.codePointAt(0); // 97 -> returns the Unicode code point for 'a'.
The codePointAt() method is not supported in Safari or Internet Explorer.
Extracting Strings
The following methods return a new string made by extracting only the characters from the passed start index to just before the end index in a String instance.
- slice()
- substring()
- substr()
var str = "abcDEFabc";
str.slice(2, 6); // cDEF -> extracts the string from index 2 through index 5.
str.slice(-4, -2); // Fa -> indexes passed as negative values start from the end of the string.
str.slice(2); // abcDEFab -> extracts to the end of the string if no end index is passed.
str.substring(2, 6); // cDEF
str.substr(2, 4); // cDEF
Splitting Strings
The following method splits a String instance by a separator and returns the split strings as one array.
- split()
If no separator is passed as an argument, the split() method returns an array of length 1 containing the entire string as a single array element.
var str = "JavaScript is very cool! And useful.";
str.split(); // does nothing if no separator is specified.
str.split(""); // splits by each character ("").
str.split(" "); // splits by spaces (" ").
str.split("!"); // splits by exclamation marks ("!").
Concatenating Strings
The following method returns a new string by concatenating the passed string to a String instance.
- concat()
var str = "JavaScript";
str; // JavaScript
str.concat(" is very cool!"); // JavaScript is very cool!
str.concat(" is very cool!", " And useful."); // JavaScript is very cool! And useful.
str; // JavaScript
In the example above, the string in variable str is still the same as at first, even after executing the concat() method several times. In this way, the value of a String instance in JavaScript is immutable. Therefore, every String method creates and returns a new string as its result.
Converting String Case
The following methods return a new string with every character in a String instance converted to uppercase or lowercase.
- toUpperCase()
- toLowerCase()
var str = "JavaScript";
str.toUpperCase(); // JAVASCRIPT
str.toLowerCase(); // javascript
Removing Whitespace Around Strings
The following method returns a new string after removing all whitespace and line break characters (LF, CR, etc.) at both ends of a String instance.
- trim()
The trim() method does not affect the string value of the String instance itself.
var str = " JavaScript ";
str.trim();
String Manipulation with Regular Expressions
The following methods use a regular expression passed as an argument to search, match, replace, and perform other operations on a String instance value.
- search()
- match()
- replace()
JavaScript String.prototype Methods
| Method | Description |
|---|---|
| indexOf() | Returns the index of the first occurrence of a specific character or string in a String instance. |
| lastIndexOf() | Returns the index of the last occurrence of a specific character or string in a String instance. |
| charAt() | Returns the character at the passed index in a String instance. |
| charCodeAt() | Returns the UTF-16 code of the character at the passed index in a String instance. (0 ~ 65535) |
| codePointAt() | Returns the Unicode code point of the character at the passed index in a String instance. |
| slice() | Extracts the string from the passed start index to just before the end index in a String instance and returns a new string. |
| substring() | Extracts the string from the passed start index to just before the end index in a String instance and returns a new string. |
| substr() | Extracts a new string of the passed length from the passed start index in a String instance. |
| split() | Splits a String instance by a separator and returns the split strings as one array. |
| concat() | Returns a new string by concatenating the passed string to a String instance. |
| toUpperCase() | Returns a new string with every character in a String instance converted to uppercase. |
| toLowerCase() | Returns a new string with every character in a String instance converted to lowercase. |
| trim() | Returns a new string after removing whitespace and all line break characters (LF, CR, etc.) at both ends of a String instance. |
| search() | Returns the index of the first occurrence of a character or string matching the regular expression passed as an argument. |
| replace() | Returns a new string by replacing the string that matches the pattern passed as an argument with a replacement string. |
| match() | Finds strings matching the regular expression passed as an argument and returns them as one array. |
| includes() | Checks whether the character or string passed as an argument is included and returns the result as a boolean value. |
| startsWith() | Checks whether the string starts with the character or string passed as an argument and returns the result as a boolean value. |
| endsWith() | Checks whether the string ends with the character or string passed as an argument and returns the result as a boolean value. |
| toLocaleUpperCase() | Returns a new string with characters from any language, not only English letters, converted to uppercase. |
| toLocaleLowerCase() | Returns a new string with characters from any language, not only English letters, converted to lowercase. |
| localeCompare() | Compares the string passed as an argument by sort order and returns the result as an integer. |
| normalize() | Returns the Unicode Normalization Form of the string. |
| repeat() | Returns a new string made by repeating and concatenating the string the number of times passed as an argument. |
| toString() | Returns the value of a String instance as a string. |
| valueOf() | Returns the value of a String instance as a string. |