Farhan9690
Farhan9690

Reputation: 57

How to check whether a certain character in string comes after another character?

Given a string aaabbbcccooeellmmzzz how can I count the number of characters that comes after 'm'. For example in this case it should return 5 (i.e oozzz);

Note: Here o and z characters come after 'm' in alphabetical order not about their index position after m.

So i want to count the characters (n,o,p,q,r,s,t,u,v,w,x,y,z) as many times they appear in the string.

Sample Input : aaabbbcccooeellmmmzzz
Sample Output: 5

Upvotes: 0

Views: 631

Answers (1)

Spectric
Spectric

Reputation: 31992

Use substring to remove everything from the start to the index of the last occurrence of the character, then get the length:

let character = 'm';
let str = 'aaabbbcccooeellmmmzzz';

let res = str.substring(str.lastIndexOf(character) + 1).length;
console.log(res)

Upvotes: 1

Related Questions