Become a Pro with JavaScript String Manipulation: Learn All About String Methods
- length(): The
length
property is a property of the JavaScriptString
object and returns the length of a string.
let str = "Hello, World!";
console.log(str.length);
// Output: 13
In the example, str
is a string with a value of "Hello, World!". The length
property returns the number of characters in the string, which is 13 in this case. The length of a string in JavaScript is represented as an integer and includes spaces, special characters, and numbers.
- indexOf(): The
indexOf()
method is a method of the JavaScriptString
object that returns the first index at which a given substring can be found in a string, or -1 if it cannot be found.
let str = "Hello, World!";
console.log(str.indexOf("Hello"));
// Output: 0
console.log(str.indexOf("world"));
// Output: -1
In the example, str
is a string with a value of "Hello, World!". The indexOf()
method is used to find the index of the substring "Hello" in the string str
.
Since "Hello" is found at the beginning of str
, the method returns 0, which is the index of the first character of "Hello". If the substring is not found in the string, the method returns -1.
In the second example, the indexOf()
method is used to find the index of the substring "world", but it is not found in the string str
, so the method returns -1.Note that the indexOf()
the method is case-sensitive, so it won't find substrings that have different capitalization. To perform a case-insensitive search, you can convert the string and the substring to lowercase using the toLowerCase()
method before using indexOf()
.
- lastIndexOf(): The
lastIndexOf()
method is a method of the JavaScriptString
object that returns the last index at which a given substring can be found in a string, or -1 if it cannot be found. Here's an example:
let str = "Hello, World! Hello";
console.log(str.lastIndexOf("Hello"));
// Output: 13
In the example, str
is a string with a value of "Hello, World! Hello". The lastIndexOf()
method is used to find the last index of the substring "Hello" in the string str
. Since "Hello" is found twice in the string, the method returns 13, which is the index of the last occurrence of "Hello". If the substring is not found in the string, the method returns -1. Like the indexOf()
method, the lastIndexOf()
method is case-sensitive. To perform a case-insensitive search, you can convert the string and the substring to lowercase using the toLowerCase()
method before using lastIndexOf()
.
- Search():The
search()
method is a method of the JavaScriptString
object that returns the first index at which a given regular expression pattern can be found in a string, or -1 if it cannot be found. Here's an example:
let str = "Hello, World!";
console.log(str.search(/Hello/));
// Output: 0
console.log(str.search(/world/i));
// Output: 7
In the example, str
is a string with a value of "Hello, World!". The search()
method is used to find the index of the regular expression pattern /Hello/
in the string str
. Since "Hello" is found at the beginning of str
, the method returns 0, which is the index of the first character of "Hello".
The second example demonstrates how to perform a case-insensitive search using the search()
method. The i
flag is used in the regular expression pattern /world/i
to indicate that the search should be case-insensitive. As a result, the search()
method returns 7, which is the index of the first character of the lowercase "world".
Note that the search()
the method is typically used when searching for a pattern that is more complex than a simple substring. For simple substring searches, it is usually more convenient to use the indexOf()
or lastIndexOf()
method.
- slice(): The
slice()
method is a method of the JavaScriptString
object that returns a portion of a string, specified by a start and end index. Here's an example:
let str = "Hello, World!";
console.log(str.slice(0, 5));
// Output: "Hello"
console.log(str.slice(7));
// Output: "World!"
In the example, str
is a string with a value of "Hello, World!". The slice()
method is used to extract a portion of the string.
The first example demonstrates how to extract a portion of the string starting from index 0 and ending at index 5. The extracted portion is “Hello”, which is the first five characters of the string.
The second example demonstrates how to extract all characters from a given index to the end of the string. In this case, the slice()
method is used to extract all characters from index 7 to the end of the string. The extracted portion is "World!", which is the portion of the string starting from index 7.
Note that the slice()
the method does not modify the original string. Instead, it returns a new string that contains the extracted portion.
- charAt(): Returns the character at a specified index in a string.
let str = "Hello, World!";
console.log(str.charAt(0));
// Output: "H"
- charCodeAt(): Returns the Unicode of the character at a specified index in a string.
let str = "Hello, World!";
console.log(str.charCodeAt(0));
// Output: 72
- concat(): Joins two or more strings and returns a new string.
let str1 = "Hello";
let str2 = ", World!";
console.log(str1.concat(str2));
// Output: "Hello, World!"
- endsWith(): Returns a Boolean indicating whether a string ends with the specified string/characters.
let str = "Hello, World!";
console.log(str.endsWith("World!"));
// Output: true
- fromCharCode(): Returns a string created from the specified sequence of Unicode values.
let char = String.fromCharCode(72, 101, 108, 108, 111);
console.log(char);
// Output: "Hello"
- includes(): Returns a Boolean indicating whether a string contains the specified string/characters.
let str = "Hello, World!";
console.log(str.includes("Hello"));
// Output: true
- match(): Searches a string for a match against a regular expression, and returns an array of the matches.
let str = "Hello, World!";
let regex = /[a-z]/gi;
console.log(str.match(regex));
// Output: ["H", "e", "l", "l", "o", "W", "o", "r", "l", "d"]
- repeat(): Returns a new string with a specified number of copies of the original string.
let str = "Hello";
console.log(str.repeat(3));
// Output: "HelloHelloHello"
- replace(): Replaces a specified value with another value in a string.
let str = "Hello, World!";
console.log(str.replace("World", "Friend"));
// Output: "Hello, Friend!"