[英]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'.给定一个字符串
aaabbbcccooeellmmzzz
,我如何计算 'm' 之后的字符数。 For example in this case it should return 5 (ie oozzz);例如在这种情况下它应该返回 5(即 oozzz);
Note: Here o and z characters come after 'm' in alphabetical order not about their index position after m.注意:这里 o 和 z 字符按字母顺序出现在“m”之后,而不是它们的索引 position 在 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.所以我想计算字符(n,o,p,q,r,s,t,u,v,w,x,y,z)在字符串中出现的次数。
Sample Input: aaabbbcccooeellmmmzzz
样本输入:
aaabbbcccooeellmmmzzz
Sample Output: 5样品 Output: 5
Use substring
to remove everything from the start to the index of the last occurrence of the character, then get the length:使用
substring
删除从开始到字符最后一次出现的索引的所有内容,然后获取长度:
let character = 'm'; let str = 'aaabbbcccooeellmmmzzz'; let res = str.substring(str.lastIndexOf(character) + 1).length; console.log(res)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.