JavaScript - Index of a Character in String
Last Updated :
17 Nov, 2024
The indexOf() method can be used to find the first index of specific character in a string. This function returns the index of the given character in a given string.
Using indexOf() - Most Used
The indexOf()
method is a method to find the index of the first occurrence of a specific string within a string.
JavaScript
let s = "abcd";
let c = "c";
let index = s.indexOf(c);
console.log(index);
Using String lastIndexOf() Method
The lastIndexOf()
method finds the index of the last occurrence of a specific character in a string. It works similarly to indexOf()
but searches from the end of the string.
JavaScript
let s = "Welcome to GeeksforGeeks";
let c = "G";
let index = s.lastIndexOf(c);
console.log(index);
Using search()
- Used for Regular Expressions
The search()
method is a powerful tool used to search for a specific substring within a string. It returns the index of the first occurrence of the specified substring.
JavaScript
let s = "abcd";
let c = "c";
let index = s.search(c);
console.log(index);
Using match() - Used for Regular Expressions and More options
The match()
method is used to search a string based on a specified pattern. It allows you to perform powerful string matching having options like global search, case insensitive search and provide detailed information.
JavaScript
let s = "abcd";
let c = "c";
let index = s.match(c);
console.log(index.index);
Writing your Own Method
In this approach, we can iterate over each character in the string using a for loop and check if it matches the specific character we are looking for. If a match is found, we return the index of that character. If no match is found, we return -1 to indicate that the character is not present in the string.
JavaScript
function findIndex(str, char) {
for (let i = 0; i < str.length; i++) {
if (str[i] === char) {
return i;
}
}
return -1; // Character not found
}
let str = "Welcome to GeeksforGeeks";
let char = "G";
let index = findIndex(str, char);
console.log(index);
Using ES6 findIndex() with Array.from() Method
In this approach, we can utilize ES6 features like Array.from() along with the findIndex() method to find the index of a specific character in a string. This method converts the string into an array of characters, then applies the findIndex() method to search for the desired character.
JavaScript
function findIndex(s, c) {
// Convert string to array of characters
let a = Array.from(s);
// Find index of the character using findIndex method
let index = a.findIndex((x) => x === c);
return index;
}
let s = "abcd";
let c = "c";
let index = findIndex(s, c);
console.log(index);
Similar Reads
JavaScript Program to Access Individual Characters in a String In this article, we are going to learn about accessing individual characters from the given string. Accessing individual characters in a string means retrieving a specific character from the string using its index position (zero-based) or iterating through each character one by one. Example: Input :
4 min read
JavaScript Program to find the Index of the First Occurrence of a Substring in a String In this article, we will find the index of the first occurrence of a substring in a string using JavaScript. An essential task in JavaScript programming is searching for substrings within a string. Finding the index of the first occurrence of a substring is a typical necessity, whether you are const
6 min read
Convert a String to a List of Characters in Java In Java, to convert a string into a list of characters, we can use several methods depending on the requirements. In this article, we will learn how to convert a string to a list of characters in Java.Example:In this example, we will use the toCharArray() method to convert a String into a character
3 min read
Convert List of Characters to String in Java Given a list of characters. In this article, we will write a Java program to convert the given list to a string. Example of List-to-String ConversionInput : list = {'g', 'e', 'e', 'k', 's'} Output : "geeks" Input : list = {'a', 'b', 'c'} Output : "abc" Strings - Strings in Java are objects that are
4 min read
JavaScript String charCodeAt() Method The JavaScript str.charCodeAt() method returns a Unicode character set code unit of the character present at the index in the string specified as the argument. The index number ranges from 0 to n-1, where n is the string's length.Syntax:str.charCodeAt(index)Parameters: This method accepts a single p
3 min read