- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Finding the immediate next character to a letter in string using JavaScript
Problem
We are required to write a JavaScript function that takes in a string of characters, str, and a single character, char.
Our function should construct a new string that contains the immediate next character present in str after each instance of char (if any).
Example
Following is the code −
const str = 'this is a string'; const letter = 'i'; const findNextString = (str = '', letter = '') => { let res = ''; for(let i = 0; i < str.length; i++){ const el = str[i]; const next = str[i + 1]; if(letter === el && next){ res += next; }; }; return res; }; console.log(findNextString(str, letter));
Output
ssn
- Related Articles
- Finding missing letter in a string - JavaScript
- Find a string such that every character is lexicographically greater than its immediate next character in Python
- Change every letter to next letter - JavaScript
- Finding the longest consecutive appearance of a character in another string using JavaScript
- Finding next prime number to a given number using JavaScript
- Finding the first non-repeating character of a string in JavaScript
- Finding the character with longest consecutive repetitions in a string and its length using JavaScript
- Finding the index of the first repeating character in a string in JavaScript
- Finding letter distance in strings - JavaScript
- Finding the immediate bigger number formed with the same digits in JavaScript
- Finding the 1-based index of a character in alphabets using JavaScript
- Finding distance to next greater element in JavaScript
- Array filtering using first string letter in JavaScript
- How to capitalize the first letter of each word in a string using JavaScript?
- Program to make vowels in string uppercase and change letters to next letter in alphabet (i.e. z->a) in JavaScript

Advertisements