Last updated: Mar 1, 2024
Reading timeยท4 min
Use the String.indexOf()
method to get the index of a character in a
string.
The String.indexOf()
method returns the index of the first occurrence of the
character or -1
if the character is not found in the string.
const str = 'hello world'; const index = str.indexOf('l'); console.log(index); // ๐๏ธ 2
We used the
String.indexOf()
method to get the index of the first occurrence of the l
character in the
string hello world
.
0
and the last character has an index of str.length - 1
.If the indexOf
method doesn't find the specified character in the string, it
returns -1
.
Use the String.lastIndexOf()
method to get the index of the last occurrence of
a character in a string.
The String.lastIndexOf()
method returns the index of the last occurrence of a
character in a string or -1
if the character is not found.
const str = 'hello world'; const lastIndex = str.lastIndexOf('l'); console.log(lastIndex); // ๐๏ธ 9
We used the String.lastIndexOf()
method to get the index of the last occurrence of the l
character in the
string hello world
.
The last l
is the 10th character in the string, so it has an index of 9
.
The lastIndexOf()
method returns -1
if the character is not found in the
string.
To get all indexes of a specific character in a string:
for
loop to iterate over the string.const str = 'hello world'; const indexes = []; for (let index = 0; index < str.length; index++) { if (str[index] === 'l') { indexes.push(index); } } console.log(indexes); // ๐๏ธ [2, 3, 9]
We declared a new variable and initialized it to an array.
The array will store the indexes of the character l
in the string
hello world
.
We used a simple for
loop to iterate for str.length
iterations.
On each iteration, we check if each character is equal to l
.
const str = 'hello world'; const indexes = []; for (let index = 0; index < str.length; index++) { if (str[index] === 'l') { indexes.push(index); } } console.log(indexes); // ๐๏ธ [2, 3, 9]
If the condition is met, we push the index into the indexes array.
0
and the last character has an index of str.length - 1
.Alternatively, you can use the String.split()
method.
This is a three-step process:
String.split()
method to split the string into an array of
characters.map()
method to iterate over the array of characters.function findIndexes(string, char) { return string .split('') .map((c, idx) => { if (c === char) { return idx; } return -1; }) .filter(element => element !== -1); } const str = 'hello world'; const indexes = findIndexes(str, 'l'); console.log(indexes); // ๐๏ธ [ 2, 3, 9 ]
We used the String.split()
method to split the string into an array of
characters.
const str = 'hello world'; // [ // 'h', 'e', 'l', 'l', // 'o', ' ', 'w', 'o', // 'r', 'l', 'd' // ] console.log(str.split(''));
The next step is to use the Array.map()
method to iterate over the array of
characters and check if each character is equal to the specified character.
const str = 'hello world'; const char = 'l'; // [ // -1, -1, 2, 3, -1, // -1, -1, -1, -1, 9, // -1 // ] console.log( str.split('').map((c, idx) => { if (c === char) { return idx; } return -1; }), );
If the current character is equal to the specified character, we return its
index, otherwise, we return -1
.
The last step is to use the Array.filter()
method to filter out all -1
values from the array.
function findIndexes(string, char) { return string .split('') .map((c, idx) => { if (c === char) { return idx; } return -1; }) .filter(element => element !== -1); } const str = 'hello world'; const indexes = findIndexes(str, 'l'); console.log(indexes); // ๐๏ธ [ 2, 3, 9 ]
The array contains the all indexes of the character in the string.
You can learn more about the related topics by checking out the following tutorials: