How to Get Character of Specific Position using JavaScript ?
Get the Character of a Specific Position Using JavaScript We have different approaches, In this article we are going to learn how to Get the Character of a Specific Position using JavaScript
Below are the methods to get the character at a specific position using JavaScript:
Table of Content
1. Using String charAt() Method
The charAt() method is used to get the character at a specific position in a string. This method takes the index as an argument and returns the character of the given index in the string.
Example: In this example, we are finding an element at index 11 using the String charAt() Method.
let str = "Welcome to GeeksforGeeks";
let index = 11;
let ctr = str.charAt(index);
console.log("Character at " + index
+ "th position is: " + ctr);
Output
Character at 11th position is: G
2. Using String substr() Method
The substr() method can also be used to get the character at a specific position in a string. This method returns the specified number of characters from a string, starting from the given index.
Example: In this example, we are finding an element at index 11 using String substr() Method
let str = "Welcome to GeeksforGeeks";
let index = 11;
let ctr = str.substr(index, 1);
console.log("Character at " + index
+ "th position is: " + ctr);
Output
Character at 11th position is: G
3. Using Square Bracket Notation
The square bracket can also be used to get the character at a specific position in a string.
Example: In this example, we are finding element at index 11 using Square Bracket Notation.
let str = "Welcome to GeeksforGeeks";
let index = 11;
let ctr = str[index];
console.log("Character at " + index
+ "th position is: " + ctr);
Output
Character at 11th position is: G
4. Using slice() Method
The string.slice() is an inbuilt method in javascript that is used to return a part or slice of the given input string.
Example: In this example, we are finding element at index 11 using slice() Method.
let str = "Welcome to GeeksforGeeks";
let index = 11;
let character = str.slice(index, index + 1);
console.log(character);
Output
G
5. Using String at() Method
The String.at() method is a modern way to get the character at a specific position in a string. This method takes an index as an argument and returns the character at the given index. One advantage of using String.at() is that it supports negative indices, which means you can easily get characters starting from the end of the string.
Example: In this example, we are finding the element at index 11 using the String.at() Method.
let str = "Welcome to GeeksforGeeks";
let index = 11;
let character = str.at(index);
console.log("Character at " + index + "th position is: " + character);
Output
Character at 11th position is: G
6. Using String.substring() Method
The substring()
method can also be used to get the character at a specific position in a string. This method returns a subset of a string between one index and another, or through the end of the string. By specifying the same start and end index, we can extract a single character.
Example:
This example demonstrates how to get the character at a specific position using the substring()
method.
let str = "Welcome to GeeksforGeeks";
let index = 11;
let character = str.substring(index, index + 1);
console.log("Character at " + index + "th position is: " + character);
Output
Character at 11th position is: G