JavaScript strings are sequences of characters and are treated as primitive data types. In JavaScript, strings are automatically converted to string objects when using string methods on them. This process is called auto-boxing. The following are methods that we can call on strings.
String Methods
slice()
Extracts a part of the string based on the given starting index and ending index and returns a new string.
// Define a string variable
let A = 'Code for Coding75';
// Use the slice() method to extract a substring
let b = A.slice(0, 5);
let c = A.slice(6, 9);
let d = A.slice(10);
// Output the value of variable
console.log(b);
console.log(c);
console.log(d);Output:
Code
for
Coding75substring()
Returns the part of the given string from the start index to the end index. Indexing starts from zero (0).
// Define a string variable
let str = "Mind, Power, Soul";
// Use the substring() method to extract a substring
let part = str.substring(6, 11);
// Output the value of variable
console.log(part);Output:
Powersubstr()
Returns the specified number of characters from the specified index from the given string.
// Define a string variable 'str'
let str = "Mind, Power, Soul";
// Use the substr() method to extract a substring
let part = str.substr(6, 5);
// Output the value of variable
console.log(part);Output:
Powerreplace()
Replaces a part of the given string with another string or a regular expression. The original string remains unchanged.
// Define a string variable 'str'
let str = "Mind, Power, Soul";
// Use the replace() method to replace the substring
let part = str.replace("Power", "Space");
// Output the resulting string after replacement
console.log(part);Output:
Mind, Space, SoulreplaceAll()
Replaces all occurrences of a specified string with another string or a regular expression.
// Define a string variable 'str'
let str = "Mind, Power, Power, Soul";
// Use the replaceAll() method to replace all occurrences
let part = str.replaceAll("Power", "Space");
// Output the resulting string after replacement
console.log(part);Output:
Mind, Space, Space, SoultoUpperCase()
Converts all the characters in the string to uppercase.
// Define a string variable
let Coders
= 'stands-for-Codin75';
// Convert the string to uppercase
console.log(Coders
.toUpperCase());Output:
STANDS-FOR-Codin75toLowerCase()
Converts all the characters in the string to lowercase.
// Define a string variable
let Coders
= 'stands-for-Coding75';
// Convert the string to lowercase
console.log(Coders
.toLowerCase());Output:
stands-for-Coding75trim()
Removes whitespace from both ends of a string.
let Coder = 'Coding ';
let newGfg = Coder.trim();
// Old length
console.log(c75.length);
// New length
console.log(newC75.length);Output:
7
3split()
Splits the string into an array of substrings.
let Coders = 'stands-for-Coding75';
// Split string on '-'
console.log(Coders.split('-'));Output:
[ 'stands', 'for', 'Coding75' ]More JS String Methods
Below is a list of JavaScript string methods:
| Method | Description |
|---|---|
at() | Find the character at the specified index. |
charAt() | Returns the character at the given index of the string. |
charCodeAt() | Returns a Unicode value of the character at the specified index. |
concat() | Joins two or more strings together. |
endsWith() | Checks if the string ends with a specified value. |
includes() | Returns true if the string contains the specified characters. |
indexOf() | Finds the index of the first occurrence of the specified value. |
lastIndexOf() | Finds the index of the last occurrence of the specified value. |
repeat() | Returns a new string containing a specified number of copies of the string. |
search() | Searches for a match between a regular expression and the string. |
slice() | Extracts a section of the string and returns a new string. |
split() | Splits a string into an array of substrings. |
startsWith() | Checks if the string starts with the specified characters. |
substring() | Extracts characters between two specified indices. |
toLowerCase() | Converts the string to lowercase. |
toUpperCase() | Converts the string to uppercase. |
trim() | Removes whitespace from both ends of a string. |
trimStart() | Removes whitespace from the start of a string. |
trimEnd() | Removes whitespace from the end of a string. |
valueOf() | Returns the primitive value of a string object. |
Conclusion
JavaScript strings are an essential part of JavaScript programming. Understanding and using these methods allows developers to efficiently manipulate strings in various ways. Mastering these methods will enhance your JavaScript skills and make string handling more effective.