How to use charAt() In Javascript
In this approach, we check if the character at the Kth index in a string is a digit. It uses the ‘charAt’ method to retrieve the character at the specified index and the ‘includes’ method to check if the character exists in the numeric string “0123456789”. The result is a boolean indicating whether the Kth element is a digit in the string.
Example: This example shows the use of the above-explained approach.
// Initializing string
let test_str = 'geeksgeeks';
// Printing original string
console.log("The original string is : " + test_str);
// initializing K
let K = 5;
// Checking if Kth character is a
// digit in the string
// Getting numeric string
let num_str = "0123456789";
let res = num_str.includes(test_str.charAt(K));
// Printing result
console.log("Is Kth element String : " + res);
Output
The original string is : geeksgeeks Is Kth element String : false
JavaScript Program to Test if Kth Character is Digit in String
Testing if the Kth character in a string is a digit in JavaScript involves checking the character at the specified index and determining if it is a numeric digit.
Examples:
Input : test_str = ‘geeks9geeks’, K = 5
Output : True
Explanation : 5th idx element is 9, a digit, hence True.
Input : test_str = ‘geeks9geeks’, K = 4
Output : False
Explanation : 4th idx element is s, not a digit, hence False.
These are the following approaches by using these we can check whether the digit is present at the Kth index or not:
Table of Content
- Using charAt():
- Using regular expression:
- Using loop
- Using codePointAt
- Using Character Comparison
- Using Array some Method